GME  13
MetaPurgeDialog.cpp
Go to the documentation of this file.
00001 // MetaPurgeDialog.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "mgautil.h"
00006 #include "MetaPurgeDialog.h"
00007 #include "atlsafe.h"
00008 
00009 #ifdef _DEBUG
00010 #define new DEBUG_NEW
00011 #undef THIS_FILE
00012 static char THIS_FILE[] = __FILE__;
00013 #endif
00014 
00016 // CMetaPurgeDialog dialog
00017 
00018 int CheckParadigms(BSTR paradigm, bool remove_invalid_entries)
00019 {
00020         int num_invalid = 0;
00021         IMgaRegistrarPtr registrar;
00022         COMTHROW(registrar.CreateInstance(__uuidof(MgaRegistrar)));
00023         CComVariant vguids;
00024         registrar->__QueryParadigmAllGUIDs(paradigm, PutOut(vguids), REGACCESS_BOTH);
00025         if (vguids.vt != (VT_ARRAY | VT_BSTR))
00026                 throw E_INVALIDARG;
00027         ATL::CComSafeArray<BSTR> guids(vguids.parray);
00028 
00029         CComBstrObj currentguid;
00030         {
00031                 CComBSTR dummy;
00032                 CComVariant vv;
00033                 registrar->__QueryParadigm(paradigm, &dummy, &vv, REGACCESS_PRIORITY);
00034                 GUID guid;
00035                 CopyTo(vv, guid);
00036                 CopyTo(guid, currentguid);
00037         }
00038 
00039         for (int i = guids.GetLowerBound(); i <= guids.GetUpperBound(); ++i)    {
00040                 GUID gg;
00041                 CopyTo(guids[i], gg);
00042                 CComVariant vRegGuid;
00043                 CopyTo(gg, vRegGuid);
00044 
00045                 _bstr_t connstr;
00046                 _bstr_t version;
00047                 TCHAR statc = 'u';
00048                 HRESULT hr = registrar->QueryParadigm(paradigm, connstr.GetAddress(), &vRegGuid, REGACCESS_USER);
00049                 if(hr == E_NOTFOUND) {
00050                         statc = 's';
00051                         hr = registrar->QueryParadigm(paradigm, connstr.GetAddress(), &vRegGuid, REGACCESS_SYSTEM);
00052                 } 
00053                 if(hr != S_OK) {
00054                         statc = ' ';
00055                         connstr = _T("<<error reading this reg.entry>>");
00056                 }
00057 
00058                 hr = registrar->VersionFromGUID(paradigm, vRegGuid, version.GetAddress(), statc ==  'u' ? REGACCESS_USER : REGACCESS_SYSTEM);
00059                 if (FAILED(hr)) {
00060                         version = _T("N/A");
00061                 }
00062 
00063                 bool valid = false;
00064                 IMgaMetaProjectPtr project;
00065                 COMTHROW(project.CreateInstance(__uuidof(MgaMetaProject)));
00066                 try
00067                 {
00068                         project->__Open(connstr);
00069                         try
00070                         {
00071                                 if (project->Version != version)
00072                                         throw _com_error(E_PROJECT_MISMATCH);
00073                                 _variant_t vFileGUID = project->GUID;
00074                                 if (vFileGUID.vt != (VT_ARRAY | VT_UI1))
00075                                         throw _com_error(E_PROJECT_MISMATCH);
00076                                 ATL::CComSafeArray<BYTE> fileGUID(vFileGUID.parray);
00077                                 if (fileGUID.GetCount() != sizeof(GUID))
00078                                         throw _com_error(E_PROJECT_MISMATCH);
00079                                 GUID gFileGUID;
00080                                 CopyTo(vFileGUID, gFileGUID);
00081                                 if (gFileGUID != gg)
00082                                         throw _com_error(E_PROJECT_MISMATCH);
00083                                 valid = true;
00084                         }
00085                         catch (_com_error&)
00086                         {
00087                                 project->__Close();
00088                                 throw;
00089                         }
00090                 }
00091                 catch (_com_error& e)
00092                 {
00093                 }
00094                 if (valid == false)
00095                 {
00096                         num_invalid++;
00097                         if (remove_invalid_entries)
00098                         {
00099                                 registrar->UnregisterParadigmGUID(paradigm, vRegGuid, statc ==  'u' ? REGACCESS_USER : REGACCESS_SYSTEM);
00100                         }
00101                 }
00102         }
00103         return num_invalid;
00104 }
00105 
00106 void CMetaPurgeDialog::OnCheckFiles()
00107 {
00108         int invalid = CheckParadigms(_bstr_t(paradigm), false);
00109         if (invalid > 0)
00110         {
00111                 wchar_t msg[256];
00112                 swprintf_s(msg, L"Found %d invalid registry entries. Do you want to remove them?", invalid);
00113                 if (IDYES == AfxMessageBox(msg, MB_YESNO))
00114                 {
00115                         CheckParadigms(_bstr_t(paradigm), true);
00116                         ResetItems();
00117                 }
00118         }
00119 }
00120 
00121 CMetaPurgeDialog::CMetaPurgeDialog(CString &paradigmname,  IMgaRegistrar *reg, CWnd* pParent /*=NULL*/)
00122         : CDialog(CMetaPurgeDialog::IDD, pParent), paradigm(paradigmname), registrar(reg)
00123 {
00124         //{{AFX_DATA_INIT(CMetaPurgeDialog)
00125         m_delfiles = -1;
00126         //}}AFX_DATA_INIT
00127 }
00128 
00129 
00130 void CMetaPurgeDialog::DoDataExchange(CDataExchange* pDX)
00131 {
00132         CDialog::DoDataExchange(pDX);
00133         //{{AFX_DATA_MAP(CMetaPurgeDialog)
00134         DDX_Control(pDX, IDC_PURGELIST, m_list);
00135         DDX_Radio(pDX, IDC_DELFILES, m_delfiles);
00136         //}}AFX_DATA_MAP
00137 }
00138 
00139 
00140 BEGIN_MESSAGE_MAP(CMetaPurgeDialog, CDialog)
00141         //{{AFX_MSG_MAP(CMetaPurgeDialog)
00142         ON_WM_CANCELMODE()
00143         ON_BN_CLICKED(IDC_PURGE2, OnPurge)
00144         ON_BN_CLICKED(IDC_SETCURRENT, OnSetcurrent)
00145         ON_BN_CLICKED(IDC_CHECK_FILES, OnCheckFiles)
00146         ON_BN_CLICKED(IDOK, OnOK)
00147         ON_NOTIFY(LVN_COLUMNCLICK, IDC_PURGELIST, OnParadigmsHeader)
00148         
00149         //}}AFX_MSG_MAP
00150 END_MESSAGE_MAP()
00151 
00153 // CMetaPurgeDialog message handlers
00154 
00155 struct SortParam {
00156         CListCtrl& list;
00157         int columnIndex;
00158 };
00159 
00160 int CALLBACK CMetaPurgeDialog::SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) {
00161         SortParam& param = *(SortParam*)lParamSort;
00162         
00163         CString a = param.list.GetItemText(lParam1, param.columnIndex);
00164         CString b = param.list.GetItemText(lParam2, param.columnIndex);
00165 
00166         return _tcsicmp(a, b);
00167 }
00168 
00169 
00170 void CMetaPurgeDialog::OnParadigmsHeader(NMHDR* pNMHDR, LRESULT* pResult) 
00171 {
00172         NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
00173         SortParam s = { this->m_list, pLV->iSubItem };
00174         m_list.SortItemsEx(SortFunc, (DWORD)(void*)&s);
00175         
00176         *pResult = 0;
00177 }
00178 
00179 
00180 BOOL CMetaPurgeDialog::OnInitDialog() 
00181 {
00182         m_delfiles = 0;
00183         CDialog::OnInitDialog();
00184 
00185         CListCtrl* listctrl = (CListCtrl*)GetDlgItem(IDC_PURGELIST);
00186     LRESULT dwStyle = listctrl->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE,0,0);
00187     dwStyle |= LVS_EX_FULLROWSELECT;
00188     listctrl->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, dwStyle);
00189 
00190         MSGTRY
00191         {
00192                 ASSERT( registrar != NULL );
00193 
00194                 LV_COLUMN lvc;
00195                 lvc.mask = LVCF_WIDTH | LVCF_TEXT;
00196 
00197                 lvc.pszText = _T("S");
00198                 lvc.cx = 20;
00199                 VERIFYTHROW( m_list.InsertColumn(0, &lvc) != -1 );
00200 
00201                 lvc.pszText = _T("Version");
00202                 lvc.cx = 50;
00203                 VERIFYTHROW( m_list.InsertColumn(1, &lvc) != -1 );
00204 
00205                 lvc.pszText = _T("GUID");
00206                 lvc.cx = 250;
00207                 VERIFYTHROW( m_list.InsertColumn(2, &lvc) != -1 );
00208 
00209                 lvc.pszText = _T("Connection String");
00210                 lvc.cx = 350;
00211                 VERIFYTHROW( m_list.InsertColumn(3, &lvc) != -1 );
00212 
00213                 ResetItems();
00214 
00215                 listctrl->SetColumnWidth(1,LVSCW_AUTOSIZE);
00216                 listctrl->SetColumnWidth(2,LVSCW_AUTOSIZE);
00217                 CRect rect;
00218                 listctrl->GetClientRect(&rect);
00219                 int width = std::max(300, rect.Width() - (listctrl->GetColumnWidth(0) + listctrl->GetColumnWidth(1) + listctrl->GetColumnWidth(2)));
00220                 listctrl->SetColumnWidth(3, width);
00221         }
00222         MSGCATCH(_T("Error while initializing MetaPurgeDlg"),;)
00223                 
00224         return TRUE;  // return TRUE unless you set the focus to a control
00225                       // EXCEPTION: OCX Property Pages should return FALSE
00226 }
00227 
00228 void CMetaPurgeDialog::OnCancelMode() 
00229 {
00230         CDialog::OnCancelMode();
00231 }
00232 
00233 void CMetaPurgeDialog::ResetItems()
00234 {
00235         ASSERT( registrar != NULL );
00236 
00237         VERIFYTHROW( m_list.DeleteAllItems() != 0 );
00238 
00239         CComVariant v;
00240         COMTHROW( registrar->QueryParadigmAllGUIDs(CComBSTR(paradigm), PutOut(v), REGACCESS_BOTH) );
00241 
00242         CComBstrObjArray guidnames;
00243         MoveTo(v, guidnames);
00244 
00245 
00246         CComBstrObj currentguid;
00247         {
00248                 CComBSTR dummy;
00249                 CComVariant vv;
00250                 COMTHROW( registrar->QueryParadigm(CComBSTR(paradigm), &dummy, &vv, REGACCESS_PRIORITY ));
00251                 GUID guid;
00252                 CopyTo(vv, guid);
00253                 CopyTo(guid, currentguid);
00254         }
00255 
00256         for(int i = 0; i < guidnames.GetSize(); ++i)    {
00257                 CString name;
00258                 CopyTo(guidnames[i], name);
00259 
00260                 GUID gg;
00261                 CopyTo(guidnames[i],gg);
00262                 CComVariant vv;
00263                 CopyTo(gg, vv);
00264 
00265                 CString connstr;
00266                 CString version;
00267                 TCHAR statc = 'u';
00268                 HRESULT hr = registrar->QueryParadigm(CComBSTR(paradigm), PutOut(connstr), &vv, REGACCESS_USER);
00269                 if(hr == E_NOTFOUND) {
00270                         statc = 's';
00271                         hr = registrar->QueryParadigm(CComBSTR(paradigm), PutOut(connstr), &vv, REGACCESS_SYSTEM);
00272                 } 
00273                 if(hr != S_OK) {
00274                         statc = ' ';
00275                         connstr = _T("<<error reading this reg.entry>>");
00276                 }
00277 
00278                 hr = registrar->VersionFromGUID(CComBSTR(paradigm), vv, PutOut(version), statc ==  'u' ? REGACCESS_USER : REGACCESS_SYSTEM);
00279                 if (FAILED(hr)) {
00280                         version = _T("N/A");
00281                 }
00282 
00283                 int j;
00284                 VERIFYTHROW( (j = m_list.InsertItem(i, CString(currentguid == guidnames[i] ? _T("*") : _T(" ")) + statc)) != -1 );
00285                 VERIFYTHROW( m_list.SetItemText(j, 1, version) != 0 );
00286                 VERIFYTHROW( m_list.SetItemText(j, 2, PutInCString(guidnames[i])) != 0 );
00287                 VERIFYTHROW( m_list.SetItemText(j, 3, connstr) != 0 );
00288 
00289         }
00290 
00291 }
00292 
00293 void CMetaPurgeDialog::OnPurge() 
00294 {
00295         UpdateData();
00296         MSGTRY
00297         {
00298                 POSITION pos = m_list.GetFirstSelectedItemPosition();
00299 
00300                 while(pos) {
00301                         int i = m_list.GetNextSelectedItem(pos);
00302                         CString cur = m_list.GetItemText(i,0);
00303                         if(cur[0] == '*') continue;
00304                         CString delguid = m_list.GetItemText(i,2);
00305                         GUID gg;
00306                         CopyTo(CComBSTR(delguid),gg);
00307                         CComVariant vv;
00308                         CopyTo(gg, vv);
00309                         COMTHROW( registrar->UnregisterParadigmGUID(CComBSTR(paradigm), vv, cur[1] == 's' ? REGACCESS_SYSTEM : REGACCESS_USER) );
00310                         CString delcstr = m_list.GetItemText(i,3);
00311                         if(m_delfiles != 0 || delcstr.Find(_T("MGA=")) != 0) continue;
00312                         DeleteFile(LPCTSTR(delcstr)+4);
00313                 }
00314 
00315                 ResetItems();
00316         }
00317         MSGCATCH(_T("Error while removing items"),;)
00318 }
00319 
00320 void CMetaPurgeDialog::OnSetcurrent() 
00321 {
00322         MSGTRY
00323         {
00324                 POSITION pos = m_list.GetFirstSelectedItemPosition();
00325 
00326                 if(pos) {
00327                         int i = m_list.GetNextSelectedItem(pos);
00328                         CString cur = m_list.GetItemText(i,0);
00329                         if(cur[0] == '*') return;
00330                         CString setver = m_list.GetItemText(i,1);
00331                         CString setguid = m_list.GetItemText(i,2);
00332                         GUID gg;
00333                         CopyTo(CComBSTR(setguid),gg);
00334                         CComVariant vv;
00335                         CopyTo(gg, vv);
00336                         CString setcstr = m_list.GetItemText(i,3);
00337                         if (setver == L"N/A")
00338                                 setver = L"";
00339                         COMTHROW( registrar->RegisterParadigm(CComBSTR(paradigm), CComBSTR(setcstr), CComBSTR(setver), vv, 
00340                                 cur[1] == 's' ? REGACCESS_SYSTEM : REGACCESS_USER) );
00341                 }
00342 
00343                 ResetItems();
00344         }
00345         MSGCATCH(_T("Error while setting current version"),;)
00346 }
00347 
00348 void CMetaPurgeDialog::OnOK()
00349 {
00350         if (m_list.GetFirstSelectedItemPosition())
00351         {
00352                 if (::MessageBox(GetSafeHwnd(), L"Do you want to purge the selected items?", L"Purge/Select", MB_YESNO) == IDYES)
00353                 {
00354                         OnPurge();
00355                 }
00356         }
00357         __super::OnOK();
00358 }
00359 
00360 void CMetaPurgeDialog::OnClose() 
00361 {
00362         CDialog::OnClose();
00363 }