GME  13
MgaOpenDlg.cpp
Go to the documentation of this file.
00001 // MgaOpenDlg.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "GMEApp.h"
00006 #include "MgaOpenDlg.h"
00007 #include "NewXmlbackendProjDlg.h"
00008 #include <afxdb.h>
00009 
00010 #ifdef _DEBUG
00011 #define new DEBUG_NEW
00012 #undef THIS_FILE
00013 static char THIS_FILE[] = __FILE__;
00014 #endif
00015 
00017 // CMgaOpenDlg dialog
00018 
00019 
00020 CMgaOpenDlg::CMgaOpenDlg(DialogTypes dType, CWnd* pParent /*=NULL*/)
00021         : CDialog(CMgaOpenDlg::IDD, pParent)
00022 {
00023         //{{AFX_DATA_INIT(CMgaOpenDlg)
00024         m_radio = 0;
00025         //}}AFX_DATA_INIT
00026 
00027         switch (dType){
00028         case OpenDialog:
00029                 title = _T("Open");
00030                 filemsg = _T("Open project file");
00031         xmlfilemsg = _T("Open multi user project");
00032                 flag_isopen = true;
00033                 flag_back = false;
00034                 flag_create = false;
00035                 flag_meta = false;
00036                 break;
00037         case SaveDialog:
00038                 title = _T("Save");
00039                 filemsg = _T("Save project file");
00040                 xmlfilemsg = _T("Save multi user project");
00041                 flag_isopen = false;
00042                 flag_back = false;
00043                 flag_create = false;
00044                 flag_meta = false;
00045                 break;
00046         case SaveAsDialog:
00047                 title = _T("Save As");
00048                 filemsg = _T("Save project file");
00049                 xmlfilemsg = _T("Save multi user project");
00050                 flag_isopen = false;
00051                 flag_back = false;
00052                 flag_create = false;
00053                 flag_meta = false;
00054                 break;
00055         case NewDialog:
00056                 title = _T("New Project");
00057                 filemsg = _T("Create project file");
00058         xmlfilemsg = _T("Create multi user project");
00059                 flag_isopen = true;
00060                 flag_back = true;
00061                 flag_create = true;
00062                 flag_meta = false;
00063                 break;
00064         case ImportDialog:
00065                 title = _T("Import to new project");
00066                 filemsg = _T("Create project file");
00067                 xmlfilemsg = _T("Create multi user project");
00068                 flag_isopen = true;
00069                 flag_back = false;
00070                 flag_create = true;
00071                 flag_meta = false;
00072                 break;
00073         case ClearLocksDialog:
00074                 title = _T("Select project");
00075                 filemsg = _T("Open project file");
00076                 xmlfilemsg = _T("Open multi user project");
00077                 flag_isopen = true;
00078                 flag_back = false;
00079                 flag_create = false;
00080                 flag_meta = false;
00081                 break;
00082         }
00083 
00084         
00085 }
00086 
00087 static TCHAR mgafilter[] = _T("MGA Files (*.mga)|*.mga|Exported Files (*.xme;*.xml)|*.xme; *.xml|")
00088         _T("All files (*.*)|*.*||");
00089 
00090 static TCHAR xmemgafilter[] = _T("GME Model Files (*.mga;*.xme;*.mgx)|*.mga; *.xme; *.mgx|MGA Files (*.mga)|*.mga|Exported Files (*.xme;*.xml)|*.xme; *.xml|")
00091         _T("Multi-user files (*.mgx)|*.mgx|All files (*.*)|*.*||");
00092 
00093 static TCHAR mgaonlyfilter[] = _T("MGA Files (*.mga)|*.mga|All files (*.*)|*.*||");
00094 
00095 static TCHAR metafilter[] = _T("MGA Meta Files (*.mta)|*.mta|XML Paradigm Files (*.xmp)|*.xmp|")
00096         _T("All files (*.*)|*.*||");
00097 
00098 
00099 CString CMgaOpenDlg::AskMGAConnectionString()
00100 {
00101         CString file, dir;
00102         if (theApp.isMgaProj() && theApp.mgaProject)
00103         {
00104                 theApp.getMgaPaths(file, dir);
00105         }
00106 
00107         CString conn;
00108         CFileDialog dlg(flag_isopen ? TRUE : FALSE, _T("mga"), (file == _T("")) ? NULL : (LPCTSTR)file, 
00109                         OFN_EXPLORER | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, mgaonlyfilter);
00110 
00111         if (dir != _T(""))
00112                 dlg.GetOFN().lpstrInitialDir = dir;
00113 
00114         if( dlg.DoModal() == IDOK )     {
00115                 conn = CString(_T("MGA=")) + dlg.GetPathName();
00116         }
00117         return conn;
00118 }
00119 
00120 CString CMgaOpenDlg::AskConnectionString(bool allowXme, bool openFileDialog)
00121 {
00122         CString conn;
00123 
00124         try
00125         {
00126                 if (openFileDialog || (DoModal() == IDOK && !pressed_back) )
00127                 {
00128                         if (openFileDialog || m_radio == 0)
00129                         {
00130                                 const TCHAR* filter;
00131                                 if (allowXme == false && openFileDialog == false)
00132                                 {
00133                                         filter = mgaonlyfilter;
00134                                 }
00135                                 else
00136                                 {
00137                                         filter = allowXme ? xmemgafilter : mgafilter;
00138                                 }
00139                                 CFileDialog dlg(openFileDialog, NULL, fileNameHint.IsEmpty() ? NULL : (LPCTSTR)fileNameHint,
00140                                         OFN_EXPLORER | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
00141                                         (flag_create ? 0 : OFN_FILEMUSTEXIST), filter);
00142                                 if (!folderPathHint.IsEmpty())
00143                                         dlg.m_ofn.lpstrInitialDir = folderPathHint.GetBuffer(_MAX_PATH);
00144                                 if (allowXme == false && openFileDialog == false)
00145                                         dlg.m_ofn.lpstrDefExt = L"mga";
00146 
00147                                 if( dlg.DoModal() == IDOK )
00148                                 {
00149                                         CString ext = dlg.GetFileExt();
00150                                         ext.MakeLower();
00151 
00152                                         if (allowXme == false && openFileDialog == false)
00153                                         {
00154                                                 return CString(L"MGA=") + dlg.GetPathName();
00155                                         }
00156                                         if( ext == _T("mga") || ext == _T("mta") )
00157                                                 conn = CString(_T("MGA=")) + dlg.GetPathName();
00158                     else if( ext == _T("mgx") )
00159                                                 conn = CString(_T("MGX=")) + dlg.GetPathName();
00160                                         else if( (ext == _T("xml")) || (ext == _T("xme")) || (ext == _T("xmp")) )
00161                                                 conn = CString(_T("XML=")) + dlg.GetPathName();
00162                                         else if( ext == _T("") )
00163                                         {
00164                                                 switch( dlg.m_ofn.nFilterIndex )
00165                                                 {
00166                                                 case 3:
00167                                                 case 1:
00168                                                         conn = CString(_T("MGA=")) + dlg.GetPathName() + _T(".mga");
00169                                                         break;
00170 
00171                                                 case 2:
00172                                                         conn = CString(_T("XML=")) + dlg.GetPathName() + _T(".xme");
00173                                                         break;
00174                                                 }
00175                                         }
00176                                         else
00177                                         {
00178                                                 switch( dlg.m_ofn.nFilterIndex )
00179                                                 {
00180                                                 case 3:
00181                                                 case 1:
00182                                                         conn = CString(_T("MGA=")) + dlg.GetPathName();
00183                                                         break;
00184 
00185                                                 case 2:
00186                                                         conn = CString(_T("XML=")) + dlg.GetPathName();
00187                                                         break;
00188                                                 }
00189                                         }
00190                                 }
00191                         }
00192             else if( m_radio == 1 )
00193             {
00194                 if( flag_create )
00195                 {
00196                     // create new multiuser project
00197                     CNewXmlbackendProjDlg dlg;
00198                     if( dlg.DoModal() == IDOK )
00199                         conn = dlg.m_connectionString;
00200                 }
00201                 else
00202                 {
00203                                         // KMS: FIXME: I think this is dead code
00204                     // open existing multiuser project
00205                     BROWSEINFO bi;
00206 
00207                     TCHAR szDisplayName[MAX_PATH];
00208                     TCHAR szPath[MAX_PATH];
00209 
00210                         bi.hwndOwner      = m_hWnd;
00211                         bi.pidlRoot       = NULL;
00212                         bi.lpszTitle      = _T("Select the project location.");
00213                         bi.pszDisplayName = szDisplayName;
00214                         bi.ulFlags        = BIF_RETURNONLYFSDIRS;
00215                         bi.lpfn           = NULL;
00216                         bi.lParam         = 0;
00217 
00218                     LPITEMIDLIST idlist = SHBrowseForFolder(&bi);
00219 
00220                     if( idlist )
00221                     {
00222                         if( SHGetPathFromIDList(idlist, szPath) ) 
00223                         {
00224                             conn = _T("MGX=\"");
00225                             conn += szPath;
00226                             conn += _T("\"");
00227                         }
00228                     }
00229                 }
00230             }
00231                 }
00232         }
00233         catch(...)
00234         {
00235         }
00236 
00237         return conn;
00238 }
00239 
00240 CString CMgaOpenDlg::PruneConnectionString(const CString& conn)
00241 {
00242         CString ret;
00243 
00244         int p = 0;
00245         while( p < conn.GetLength() )
00246         {
00247                 int q = conn.Find(';', p);
00248                 if( q < 0 )
00249                         q = conn.GetLength();
00250 
00251                 CString part((const TCHAR*)conn + p, q-p);
00252 
00253                 int r = part.Find('=');
00254                 if( r < 0 )
00255                         r = part.GetLength();
00256 
00257                 CString key((const TCHAR*)part, r);
00258 
00259                 if( key == _T("UID") ||
00260                         key == _T("PWD") ||
00261                         key == _T("USER") ||
00262                         key == _T("PASSWORD") )
00263                 {
00264                         if( !ret.IsEmpty() )
00265                                 ret += _T(";");
00266 
00267                         ret += part;
00268                 }
00269 
00270                 p = q+1;
00271         }
00272 
00273         return ret;
00274 }
00275 
00276 CString CMgaOpenDlg::FilterInvalidCharacters(const CString& path, bool isPath)
00277 {
00278         CString str = path;
00279         // are there invalid characters in the suggested file name?
00280         if (str.FindOneOf( _T("\\/:*?\"<>|")) != -1)
00281         {
00282                 TCHAR nc = _T('_');
00283                 int rpl = 0;
00284                 if (!isPath) {
00285                         rpl += str.Replace(_T('\\'), nc);
00286                         rpl += str.Replace(_T('/'), nc);
00287                         rpl += str.Replace(_T(':'), nc);
00288                 }
00289                 rpl += str.Replace(_T('*'), nc);
00290                 rpl += str.Replace(_T('?'), nc);
00291                 rpl += str.Replace(_T('"'), nc);
00292                 rpl += str.Replace(_T('<'), nc);
00293                 rpl += str.Replace(_T('>'), nc);
00294                 rpl += str.Replace(_T('|'), nc);
00295                 // rpl chars replaced in total
00296         }
00297         return str;
00298 }
00299 
00300 void CMgaOpenDlg::DoDataExchange(CDataExchange* pDX)
00301 {
00302         CDialog::DoDataExchange(pDX);
00303         //{{AFX_DATA_MAP(CMgaOpenDlg)
00304         DDX_Radio(pDX, IDC_RADIO2, m_radio);
00305         //}}AFX_DATA_MAP
00306 }
00307 
00308 BEGIN_MESSAGE_MAP(CMgaOpenDlg, CDialog)
00309         //{{AFX_MSG_MAP(CMgaOpenDlg)
00310         ON_BN_CLICKED(IDC_BACK, OnBack)
00311         ON_BN_CLICKED(IDC_NEXT, OnNext)
00312         //}}AFX_MSG_MAP
00313 END_MESSAGE_MAP()
00314 
00316 // CMgaOpenDlg message handlers
00317 
00318 
00319 BOOL CMgaOpenDlg::OnInitDialog() 
00320 {
00321         CDialog::OnInitDialog();
00322 
00323         GetDlgItem(IDC_BACK)->EnableWindow(flag_back);
00324         SetWindowText(title);
00325         GetDlgItem(IDC_RADIO2)->SetWindowText(filemsg);
00326     GetDlgItem(IDC_RADIO4)->SetWindowText(xmlfilemsg);
00327 #if defined(_M_X64)
00328         GetDlgItem(IDC_RADIO4)->EnableWindow(false);
00329 #endif
00330 
00331         if( flag_meta )
00332         {
00333                 GetDlgItem(IDC_MGAOPEN_BMP)->ShowWindow(SW_HIDE);
00334                 GetDlgItem(IDC_METAOPEN_BMP)->ShowWindow(SW_SHOW);
00335         }
00336         else
00337         {
00338                 GetDlgItem(IDC_MGAOPEN_BMP)->ShowWindow(SW_SHOW);
00339                 GetDlgItem(IDC_METAOPEN_BMP)->ShowWindow(SW_HIDE);
00340         }
00341 
00342         return TRUE;  // return TRUE unless you set the focus to a control
00343                       // EXCEPTION: OCX Property Pages should return FALSE
00344 }
00345 
00346 void CMgaOpenDlg::OnBack() 
00347 {
00348         // TODO: Add your control notification handler code here
00349 
00350         pressed_back = true;
00351         CDialog::OnOK();
00352 }
00353 
00354 
00355 void CMgaOpenDlg::OnNext() 
00356 {
00357         // TODO: Add your control notification handler code here
00358 
00359         pressed_back = false;
00360         CDialog::OnOK();
00361 }
00362 
00363 void CMgaOpenDlg::SetFileNameHint(const CString& hint)
00364 {
00365         fileNameHint = FilterInvalidCharacters(hint);
00366 }
00367 
00368 void CMgaOpenDlg::SetFolderPathHint(const CString& hint)
00369 {
00370         folderPathHint = FilterInvalidCharacters(hint, true);
00371 }