00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007
00008 #include "FdmApp.h"
00009 #include "dbghelp.h"
00010
00011 #include "MainFrm.h"
00012 #include "UrlWnd.h"
00013 #include <initguid.h>
00014 #include "WGUrlReceiver.h"
00015 #include "Fdm_i.c"
00016 #include "UEDlg.h"
00017 #include "fsIEUserAgent.h"
00018 #include "WgUrlListReceiver.h"
00019 #include "CFDM.h"
00020 #include "fsFDMCmdLineParser.h"
00021 #include "mfchelp.h"
00022 #include "vmsFilesToDelete.h"
00023 #include "vmsDLL.h"
00024 #include "vmsFirefoxMonitoring.h"
00025 #include "FDMDownloadsStat.h"
00026 #include "FDMDownload.h"
00027 #include "FDMUploader.h"
00028 #include "FDMUploadPackage.h"
00029 #include "vmsTorrentExtension.h"
00030 #include "FdmTorrentFilesRcvr.h"
00031 #include "FDMFlashVideoDownloads.h"
00032 #include "vmsUploadsDllCaller.h"
00033 #include "vistafx/vistafx.h"
00034 #include "inetutil.h"
00035
00036 #ifdef _DEBUG
00037 #define new DEBUG_NEW
00038 #undef THIS_FILE
00039 static char THIS_FILE[] = __FILE__;
00040 #endif
00041
00042 BEGIN_MESSAGE_MAP(CFdmApp, CWinApp)
00043
00044 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
00045
00046 END_MESSAGE_MAP()
00047
00048 CFdmApp::CFdmApp()
00049 {
00050 m_bCOMInited = m_bATLInited = m_bATLInited2 = FALSE;
00051 m_bSaveAllOnExit = FALSE;
00052 m_hAppMutex = NULL;
00053 m_bEmbedding = FALSE;
00054 m_bStarting = TRUE;
00055
00056 SYSTEMTIME time;
00057 GetLocalTime (&time);
00058 SystemTimeToFileTime (&time, &_timeAppHasStarted);
00059
00060 m_pModuleState;
00061 }
00062
00063 CFdmApp theApp;
00064
00065 BOOL CFdmApp::InitInstance()
00066 {
00067 SetUnhandledExceptionFilter (_UEF);
00068
00069 AfxEnableControlContainer ();
00070
00071 SetRegistryKey (IDS_COMPANY);
00072
00073 #ifdef _USELOGGING
00074 char szL [MAX_PATH];
00075 GetModuleFileName (NULL, szL, MAX_PATH);
00076 CString strLogFile = szL;
00077 fsGetPath (strLogFile, szL);
00078 strLogFile = szL;
00079 if (strLogFile.Right (1) != "\\")
00080 strLogFile += "\\";
00081 strLogFile += "fdm.log";
00082 _Log.Initialize (strLogFile);
00083 _Log.EraseLog ();
00084 #endif
00085
00086 LOG ("========================================================" << nl);
00087 LOG ("starting..." << nl);
00088 LOG ("initializing..." << nl);
00089
00090 CheckRegistry ();
00091
00092 m_bSaveAllOnExit = FALSE;
00093
00094 fsIECatchMgr::CleanIEPluginKey ();
00095
00096 CString strPath = GetProfileString ("", "Path", "");
00097 BOOL bNeedLocalRegister = FALSE;
00098 if (strPath == "")
00099 {
00100 CRegKey key;
00101 if (ERROR_SUCCESS == key.Open (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Download Manager", KEY_WRITE))
00102 vmsSHCopyKey (HKEY_LOCAL_MACHINE, "Software\\OpenDownloadManager.ORG\\Open Download Manager", key);
00103 strPath = GetProfileString ("", "Path", "");
00104 bNeedLocalRegister = strPath != "";
00105 }
00106
00107 if (strPath == "" || FALSE == SetCurrentDirectory (strPath))
00108 _dwAppState |= APPSTATE_PORTABLE_MODE;
00109
00110 char szExeDir [MY_MAX_PATH], szExeFile [MY_MAX_PATH];
00111 GetModuleFileName (NULL, szExeFile, sizeof (szExeFile));
00112 fsGetPath (szExeFile, szExeDir);
00113
00114 if (IS_PORTABLE_MODE)
00115 {
00116 strPath = szExeDir;
00117 SetCurrentDirectory (strPath);
00118 }
00119
00120 m_strAppPath = strPath;
00121 if (m_strAppPath.IsEmpty () == FALSE)
00122 {
00123 if (m_strAppPath [m_strAppPath.GetLength ()-1] != '\\' &&
00124 m_strAppPath [m_strAppPath.GetLength ()-1] != '/')
00125 m_strAppPath += '\\';
00126 }
00127
00128 if (IS_PORTABLE_MODE == false)
00129 {
00130 CString strDataFldr = szExeDir; strDataFldr += "Data";
00131
00132 if (m_strAppPath.CompareNoCase (szExeDir) &&
00133 DWORD (-1) != GetFileAttributes (strDataFldr))
00134 {
00135 _dwAppState |= APPSTATE_PORTABLE_MODE;
00136 _dwAppState |= APPSTATE_PORTABLE_MODE_NOREG;
00137 m_strAppPath = szExeDir;
00138 }
00139 }
00140
00141 if (IS_PORTABLE_MODE)
00142 {
00143 vmsAppSettingsStore* pStgs = _App.get_SettingsStore ();
00144 CString strStgsFile = m_strAppPath + "Data\\settings.dat";
00145 fsBuildPathToFile (strStgsFile);
00146 pStgs->LoadSettingsFromFile (strStgsFile);
00147 }
00148
00149 BOOL bNoLng = FALSE;
00150
00151 if (FALSE == InitLanguage ())
00152 bNoLng = TRUE;
00153
00154 if (strcmp (m_lpCmdLine, "-suis") == 0 ||
00155 strcmp (m_lpCmdLine, "-euis") == 0 ||
00156 strcmp (m_lpCmdLine, "-duis") == 0)
00157 {
00158 IntegrationSettings ();
00159 return FALSE;
00160 }
00161
00162 if (IS_PORTABLE_MODE)
00163 {
00164 char szTmpFile [MY_MAX_PATH];
00165 CString str = m_strAppPath; str += "Data";
00166 CreateDirectory (str, NULL);
00167 if (0 == GetTempFileName (str, "fdm", 0, szTmpFile))
00168 MessageBox (NULL, LS (L_NOWRITEACCESSTODATAFOLDER), PRG_NAME, MB_ICONWARNING);
00169 else
00170 DeleteFile (szTmpFile);
00171 }
00172
00173 _SkinMgr.Initialize ();
00174
00175 _IECatchMgr.ReadSettings ();
00176 _NOMgr.Initialize ();
00177 _IECMM.ReadState ();
00178
00179 HRESULT hRes = OleInitialize (NULL);
00180
00181 if (FAILED(hRes))
00182 return FALSE;
00183
00184 m_bCOMInited = TRUE;
00185
00186 fsFDMCmdLineParser cmdline;
00187 cmdline.Parse ();
00188 m_bForceSilentSpecified = cmdline.is_ForceSilentSpecified ();
00189
00190 if (GetVersion () & 0x80000000)
00191 {
00192
00193 CFileFind f;
00194 BOOL b = f.FindFile (m_strAppPath + "*.sav");
00195 std::vector <CString> v;
00196 while (b)
00197 {
00198 b = f.FindNextFile ();
00199 v.push_back (f.GetFileName ());
00200 }
00201 for (size_t i = 0; i < v.size (); i++)
00202 MoveFile (m_strAppPath + v [i], fsGetDataFilePath (v [i]));
00203 }
00204 MoveFile (fsGetDataFilePath ("uploads.sav"), fsGetDataFilePath ("uploads.1.sav"));
00205
00206 if (!InitATL())
00207 return FALSE;
00208
00209 if (VistaFx::IsVista () && strncmp (m_lpCmdLine, "-nelvcheck", 10) && stricmp (m_lpCmdLine, "-autorun"))
00210 {
00211 VistaFx vf;
00212 vf.LoadDll (m_strAppPath + "vistafx.dll");
00213 if (vf.IsProcessElevated ())
00214 {
00215 char sz [MAX_PATH];
00216 GetModuleFileName (NULL, sz, MAX_PATH);
00217 CString str = "-nelvcheck ";
00218 str += m_lpCmdLine;
00219 if (m_hAppMutex)
00220 {
00221 CloseHandle (m_hAppMutex);
00222 m_hAppMutex = NULL;
00223 }
00224 if (vf.RunNonElevatedProcess (sz, str, ""))
00225 return FALSE;
00226 m_hAppMutex = CreateMutex (NULL, TRUE, _pszAppMutex);
00227 }
00228 }
00229
00230 if (IS_PORTABLE_MODE && (_dwAppState & APPSTATE_PORTABLE_MODE_NOREG) == 0)
00231 Install_RegisterServer ();
00232
00233 vmsFilesToDelete::Process ();
00234
00235 if (bNeedLocalRegister)
00236 RegisterServer (FALSE);
00237
00238 #ifdef _AFXDLL
00239 Enable3dControls();
00240 #else
00241 Enable3dControlsStatic();
00242 #endif
00243
00244 CheckLocked ();
00245
00246 m_bSaveAllOnExit = TRUE;
00247
00248 _UpdateMgr.ReadSettings ();
00249
00250 if (_UpdateMgr.IsStartUpdaterNeeded ())
00251 {
00252 if (_UpdateMgr.StartUpdater ())
00253 return FALSE;
00254 else
00255 ::MessageBox (NULL, LS (L_CANTFINDUPDATER), LS (L_ERR), MB_ICONERROR);
00256 }
00257
00258 LoadHistory ();
00259
00260 _Snds.ReadSettings ();
00261
00262 if (_App.ModifyIEUserAgent ())
00263 {
00264 fsIEUserAgent ua;
00265 ua.SetPP (IE_USERAGENT_ADDITION);
00266 ua.RemovePP ("Open Download Manager");
00267 }
00268
00269 CMainFrame* pFrame = NULL;
00270 fsnew1 (pFrame, CMainFrame);
00271 m_pMainWnd = pFrame;
00272
00273 LOG ("creating main window and client area..." << nl);
00274
00275 if (FALSE == pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,
00276 NULL, NULL))
00277 {
00278 return FALSE;
00279 }
00280
00281 LOG ("main window and client area was created" << nl);
00282
00283 _App.View_ReadWndPlacement (pFrame, "MainFrm");
00284
00285 if (_tcscmp (m_lpCmdLine, _T ("-autorun")) == 0)
00286 pFrame->ShowWindow (SW_HIDE);
00287 else if (_App.Prg_StartMinimized ())
00288 {
00289 if (IsWindowVisible (pFrame->m_hWnd))
00290 pFrame->ShowWindow (SW_MINIMIZE);
00291 }
00292 else
00293 {
00294 pFrame->UpdateWindow();
00295 if (pFrame->IsWindowVisible ())
00296 pFrame->SetForegroundWindow ();
00297 }
00298
00299 m_bStarting = FALSE;
00300
00301 hRes = _Module.RegisterClassObjects (CLSCTX_LOCAL_SERVER,
00302 REGCLS_MULTIPLEUSE);
00303 if (FAILED (hRes))
00304 {
00305 LPVOID lpMsgBuf;
00306 FormatMessage(
00307 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00308 FORMAT_MESSAGE_FROM_SYSTEM |
00309 FORMAT_MESSAGE_IGNORE_INSERTS,
00310 NULL,
00311 hRes,
00312 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00313 (LPTSTR) &lpMsgBuf,
00314 0,
00315 NULL
00316 );
00317
00318 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
00319
00320 LocalFree( lpMsgBuf );
00321 }
00322 m_bATLInited2 = SUCCEEDED (hRes);
00323
00324 return TRUE;
00325 }
00326
00327 class CAboutDlg : public CDialog
00328 {
00329 public:
00330 CAboutDlg();
00331
00332
00333 enum { IDD = IDD_ABOUTBOX };
00334 CUrlWnd m_wndDLURL;
00335 CUrlWnd m_wndFirm;
00336 CUrlWnd m_wndFirm1;
00337
00338
00339
00340
00341 protected:
00342 virtual void DoDataExchange(CDataExchange* pDX);
00343
00344
00345 protected:
00346 CFont m_fntUnderline;
00347 CFont m_fontRegInfo;
00348 CFont m_fontWarn;
00349 CUrlWnd m_wndSupport;
00350
00351 virtual BOOL OnInitDialog();
00352 afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
00353
00354 DECLARE_MESSAGE_MAP()
00355 };
00356
00357 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
00358 {
00359
00360
00361 m_fontRegInfo.CreateFont (10, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, "MS Sans Serif");
00362 m_fontWarn.CreateFont (12, 0, 0, 0, FW_LIGHT, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, "Arial");
00363 }
00364
00365 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
00366 {
00367 CDialog::DoDataExchange(pDX);
00368
00369
00370
00371 }
00372
00373 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
00374
00375 ON_WM_CTLCOLOR()
00376
00377 END_MESSAGE_MAP()
00378
00379 void CFdmApp::OnAppAbout()
00380 {
00381 CAboutDlg aboutDlg;
00382 _DlgMgr.OnDoModal (&aboutDlg);
00383 aboutDlg.DoModal();
00384 _DlgMgr.OnEndDialog (&aboutDlg);
00385 }
00386
00387 int CFdmApp::ExitInstance()
00388 {
00389 LOG ("shutting down..." << nl);
00390
00391 if (m_bATLInited2)
00392 {
00393 LOG ("deinitializing ATL...");
00394 _Module.RevokeClassObjects();
00395 }
00396
00397 if (IS_PORTABLE_MODE && (_dwAppState & APPSTATE_PORTABLE_MODE_NOREG) == 0)
00398 Install_UnregisterServer ();
00399
00400 if (m_bATLInited)
00401 {
00402 _Module.Term();
00403 LOG ("done." << nl);
00404 }
00405
00406 if (m_bSaveAllOnExit)
00407 {
00408 SaveSettings ();
00409 SaveHistory ();
00410 _UpdateMgr.SaveSettings ();
00411 }
00412
00413 LOG ("exit now" << nl);
00414 LOG ("========================================================" << nl);
00415
00416 return CWinApp::ExitInstance();
00417 }
00418
00419 BOOL CAboutDlg::OnInitDialog()
00420 {
00421 CDialog::OnInitDialog();
00422
00423 CFont* fnt = GetFont ();
00424 LOGFONT lf;
00425 fnt->GetLogFont (&lf);
00426 lf.lfUnderline = TRUE;
00427 m_fntUnderline.CreateFontIndirect (&lf);
00428
00429 m_wndDLURL.SubclassDlgItem (IDC__DOWNLOAD, this);
00430 m_wndDLURL.Init ();
00431 m_wndDLURL.SetUrl ("http://www.opendownloadmanager.org/download/");
00432
00433 CString strVer;
00434 char szVer [] = "%s %s build %d";
00435 strVer.Format (szVer, LS (L_VERSION), PRG_VERSION, PRG_BUILD_NUMBER);
00436 SetDlgItemText (IDC__VER, strVer);
00437
00438 m_wndFirm.SubclassDlgItem (IDC__FIRM, this);
00439 m_wndFirm.Init ();
00440 m_wndFirm.SetUrl ("http://www.freedownloadmanager.org");
00441
00442 m_wndFirm1.SubclassDlgItem (IDC__FIRM2, this);
00443 m_wndFirm1.Init ();
00444 m_wndFirm1.SetUrl ("http://www.opendownloadmanager.org");
00445
00446 SetDlgItemText (IDC__SUPPORT, LS (L_SUPPORTANDOTHER));
00447
00448 m_wndSupport.SubclassDlgItem (IDC__SUPPORT, this);
00449 m_wndSupport.Init ();
00450 m_wndSupport.SetUrl ("http://www.opendownloadmanager.org/support/");
00451
00452 SetDlgItemText (IDC__DOWNLOAD, LS (L_DLLATESTVERSION));
00453
00454 SetDlgItemText (IDC__WARN, LS (L_LICWARN));
00455
00456 SetWindowText (LS (L_ABOUT2));
00457
00458 if (_AppMgr.IsBtInstalled () == FALSE || _AppMgr.IsMediaFeaturesInstalled () == FALSE)
00459 {
00460 CString str;
00461 GetDlgItemText (IDC__NAME, str);
00462 str += " (Light)";
00463 SetDlgItemText (IDC__NAME, str);
00464 }
00465
00466 return TRUE;
00467 }
00468
00469 HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
00470 {
00471 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
00472
00473 if (pWnd->m_hWnd == m_wndFirm.m_hWnd ||
00474 pWnd->m_hWnd == m_wndFirm1.m_hWnd ||
00475 pWnd->m_hWnd == m_wndDLURL.m_hWnd ||
00476 pWnd->m_hWnd == m_wndSupport.m_hWnd)
00477 {
00478 pDC->SetTextColor (GetSysColor (26));
00479 pDC->SelectObject (&m_fntUnderline);
00480 }
00481
00482 if (pWnd->m_hWnd == GetDlgItem (IDC__WARN)->m_hWnd)
00483 pDC->SelectObject (&m_fontWarn);
00484
00485 return hbr;
00486 }
00487
00488 void CFdmApp::LoadHistory()
00489 {
00490 LOG ("loading history file...");
00491
00492 _HsMgr.ReadSettings ();
00493
00494 HANDLE hFile = CreateFile (fsGetDataFilePath ("history.sav"), GENERIC_READ, FILE_SHARE_READ, NULL,
00495 OPEN_EXISTING, 0, NULL);
00496
00497 if (hFile == INVALID_HANDLE_VALUE)
00498 {
00499 if (GetLastError () != ERROR_FILE_NOT_FOUND)
00500 goto _lErr;
00501 LOG ("done (no history file found)" << nl);
00502 return;
00503 }
00504
00505 if (!_LastUrlFiles.ReadFromFile (hFile))
00506 goto _lErr;
00507
00508
00509 if (!_LastUrlPaths.ReadFromFile (hFile))
00510 goto _lErr;
00511
00512
00513 if (!_LastFolders.ReadFromFile (hFile))
00514 goto _lErr;
00515
00516
00517 if (!_LastBatchUrls.ReadFromFile (hFile))
00518 goto _lErr;
00519
00520
00521 if (!_LastFind.ReadFromFile (hFile))
00522 goto _lErr;
00523
00524 if (!_LastFlashVideoUrls.ReadFromFile (hFile))
00525 goto _lErr;
00526
00527 CloseHandle (hFile);
00528
00529 LOG ("ok." << nl);
00530
00531 return;
00532
00533 _lErr:
00534 LOG ("failed (error #" << GetLastError () << ")" << nl);
00535 if (hFile != INVALID_HANDLE_VALUE)
00536 CloseHandle (hFile);
00537 }
00538
00539 void CFdmApp::SaveHistory()
00540 {
00541 HANDLE hFile = CreateFile (fsGetDataFilePath ("history.sav"), GENERIC_WRITE, 0, NULL,
00542 CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
00543
00544 LOG ("saving history...");
00545
00546 if (hFile == INVALID_HANDLE_VALUE)
00547 {
00548 goto _lErr;
00549 }
00550
00551 if (!_LastUrlFiles.SaveToFile (hFile))
00552 goto _lErr;
00553
00554 if (!_LastUrlPaths.SaveToFile (hFile))
00555 goto _lErr;
00556
00557 if (!_LastFolders.SaveToFile (hFile))
00558 goto _lErr;
00559
00560 if (!_LastBatchUrls.SaveToFile (hFile))
00561 goto _lErr;
00562
00563 if (!_LastFind.SaveToFile (hFile))
00564 goto _lErr;
00565
00566 if (!_LastFlashVideoUrls.SaveToFile (hFile))
00567 goto _lErr;
00568
00569 CloseHandle (hFile);
00570
00571 LOG ("ok." << nl);
00572
00573 return;
00574
00575 _lErr:
00576 LOG ("failed (error #" << GetLastError () << ")" << nl);
00577
00578 if (hFile != INVALID_HANDLE_VALUE)
00579 CloseHandle (hFile);
00580
00581 DeleteFile (fsGetDataFilePath ("history.sav"));
00582 }
00583
00584 CFdmModule _Module;
00585
00586 BEGIN_OBJECT_MAP(ObjectMap)
00587 OBJECT_ENTRY(CLSID_WGUrlReceiver, CWGUrlReceiver)
00588 OBJECT_ENTRY(CLSID_WGUrlListReceiver, CWgUrlListReceiver)
00589 OBJECT_ENTRY(CLSID_FDM, CFDM)
00590 OBJECT_ENTRY(CLSID_FDMDownloadsStat, CFDMDownloadsStat)
00591 OBJECT_ENTRY(CLSID_FDMDownload, CFDMDownload)
00592 OBJECT_ENTRY(CLSID_FDMUploader, CFDMUploader)
00593 OBJECT_ENTRY(CLSID_FDMUploadPackage, CFDMUploadPackage)
00594 OBJECT_ENTRY(CLSID_FdmTorrentFilesRcvr, CFdmTorrentFilesRcvr)
00595 OBJECT_ENTRY(CLSID_FDMFlashVideoDownloads, CFDMFlashVideoDownloads)
00596 END_OBJECT_MAP()
00597
00598 LONG CFdmModule::Unlock()
00599 {
00600 AfxOleUnlockApp();
00601 return 0;
00602 }
00603
00604 LONG CFdmModule::Lock()
00605 {
00606 AfxOleLockApp();
00607 return 1;
00608 }
00609 LPCTSTR CFdmModule::FindOneOf(LPCTSTR p1, LPCTSTR p2)
00610 {
00611 while (*p1 != NULL)
00612 {
00613 LPCTSTR p = p2;
00614 while (*p != NULL)
00615 {
00616 if (*p1 == *p)
00617 return CharNext(p1);
00618 p = CharNext(p);
00619 }
00620 p1++;
00621 }
00622 return NULL;
00623 }
00624
00625 BOOL CFdmApp::InitATL()
00626 {
00627 m_bEmbedding = FALSE;
00628
00629 LPTSTR lpCmdLine = GetCommandLine();
00630 TCHAR szTokens[] = _T("-/");
00631
00632 BOOL bRun = TRUE;
00633 LPCTSTR lpszToken = _Module.FindOneOf(lpCmdLine, szTokens);
00634
00635 while (lpszToken != NULL)
00636 {
00637 if (lstrcmpi(lpszToken, _T("Embedding"))==0)
00638 {
00639
00640 }
00641
00642 if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
00643 {
00644 BOOL bWaitShutdown = FALSE;
00645 if (CheckFdmStartedAlready (FALSE))
00646 {
00647 UINT nMsg = RegisterWindowMessage ("ODM - shutdown");
00648 if (nMsg)
00649 {
00650 PostMessage (HWND_BROADCAST, nMsg, 0, 0);
00651 bWaitShutdown = TRUE;
00652 }
00653 else
00654 {
00655 MessageBox (NULL, "Unable to shutdown Open Download Manager.\nPlease do that yourself.", "Error", MB_ICONEXCLAMATION);
00656 }
00657 }
00658
00659 HANDLE hMxWi = CreateMutex (NULL, FALSE, "ODM - remote control server");
00660 if (GetLastError () == ERROR_ALREADY_EXISTS)
00661 {
00662 UINT nMsg = RegisterWindowMessage ("ODM - remote control server - shutdown");
00663 if (nMsg)
00664 PostMessage (HWND_BROADCAST, nMsg, 0, 0);
00665 }
00666 if (hMxWi)
00667 CloseHandle (hMxWi);
00668
00669 if (bWaitShutdown)
00670 {
00671 while (CheckFdmStartedAlready (FALSE))
00672 {
00673 Sleep (400);
00674 }
00675 }
00676
00677 Install_UnregisterServer ();
00678 bRun = FALSE;
00679 break;
00680 }
00681 if (lstrcmpi(lpszToken, _T("RegServer"))==0)
00682 {
00683 Install_RegisterServer ();
00684 bRun = FALSE;
00685 break;
00686 }
00687
00688 lpszToken = _Module.FindOneOf(lpszToken, szTokens);
00689 }
00690
00691 if (bRun && CheckFdmStartedAlready (m_bForceSilentSpecified == FALSE))
00692 bRun = FALSE;
00693
00694 if (!bRun)
00695 return FALSE;
00696
00697 if (m_bATLInited == FALSE)
00698 {
00699 m_bATLInited = TRUE;
00700 _Module.Init (ObjectMap, AfxGetInstanceHandle());
00701 _Module.dwThreadID = GetCurrentThreadId ();
00702 }
00703
00704 return TRUE;
00705 }
00706
00707 BOOL CFdmApp::InitLanguage()
00708 {
00709 LOG ("initializing language..." << nl);
00710
00711 m_nNoLngsErrReason = 0;
00712
00713 if (FALSE == _LngMgr.Initialize ())
00714 {
00715 LOG ("failed : no files found" << nl);
00716 m_nNoLngsErrReason = 1;
00717 return FALSE;
00718 }
00719
00720 LOG ("cDSA::IL: cl: " << _App.View_Language () << nl);
00721
00722
00723 int iLang = _LngMgr.FindLngByName (_App.View_Language ());
00724 if (iLang == -1)
00725 {
00726 LOG ("cDSA::IL: cl: not found" << nl);
00727 iLang = 0;
00728 }
00729
00730 if (FALSE == _LngMgr.LoadLng (iLang))
00731 {
00732 LOG ("cDSA::IL: cl: failed to load" << nl);
00733
00734 if (iLang == 0 || FALSE == _LngMgr.LoadLng (0))
00735 {
00736 m_nNoLngsErrReason = 2;
00737 LOG ("failed : no valid lang files found" << nl);
00738 return FALSE;
00739 }
00740 }
00741
00742 LOG ("ok." << nl);
00743 return TRUE;
00744 }
00745
00746 BOOL CFdmApp::CheckFdmStartedAlready(BOOL bSetForIfEx)
00747 {
00748 LPCSTR pszMainWnd = "Open Download Manager Main Window";
00749
00750 LOG ("checking ODM started already...");
00751
00752 m_hAppMutex = CreateMutex (NULL, TRUE, _pszAppMutex);
00753
00754 if (GetLastError () == ERROR_ALREADY_EXISTS)
00755 {
00756 CloseHandle (m_hAppMutex);
00757 m_hAppMutex = NULL;
00758
00759 LOG ("ODM detected" << nl);
00760
00761 if (bSetForIfEx)
00762 {
00763
00764 HWND hWnd = FindWindow (pszMainWnd, NULL);
00765
00766 LOG ("ODM window: " << (DWORD)hWnd);
00767
00768 if (IsIconic (hWnd))
00769 {
00770 LOG (" is iconic" << nl);
00771 ShowWindow (hWnd, SW_RESTORE);
00772 }
00773 else
00774 {
00775 LOG (" is not iconic" << nl);
00776 WINDOWPLACEMENT wc;
00777 GetWindowPlacement (hWnd, &wc);
00778 LOG ("#cp1" << nl);
00779 if (wc.showCmd == SW_HIDE)
00780 ShowWindow (hWnd, SW_RESTORE);
00781 }
00782
00783 LOG ("#cp2" << nl);
00784 SetForegroundWindow (hWnd);
00785 LOG ("#cp3" << nl);
00786 SetFocus (hWnd);
00787 LOG ("#cp4" << nl);
00788 }
00789
00790 return TRUE;
00791 }
00792
00793 LOG ("ok." << nl);
00794
00795 return FALSE;
00796 }
00797
00798 CFdmApp::~CFdmApp()
00799 {
00800 if (m_bCOMInited)
00801 CoUninitialize();
00802 }
00803
00804 LONG CFdmApp::_UEF(_EXCEPTION_POINTERS *info)
00805 {
00806 static BOOL _b = FALSE;
00807
00808 if (_b)
00809 return EXCEPTION_EXECUTE_HANDLER;
00810
00811 _b = TRUE;
00812
00813 typedef BOOL (WINAPI *FNMDWD)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
00814 PMINIDUMP_EXCEPTION_INFORMATION, PMINIDUMP_USER_STREAM_INFORMATION, PMINIDUMP_CALLBACK_INFORMATION);
00815 vmsDLL dll ("dbghelp.dll");
00816 FNMDWD pfnMiniDumpWriteDump;
00817 pfnMiniDumpWriteDump = (FNMDWD) dll.GetProcAddress ("MiniDumpWriteDump");
00818 if (pfnMiniDumpWriteDump == NULL)
00819 return EXCEPTION_EXECUTE_HANDLER;
00820
00821 LOG ("Unhandled Exception was raised !!!" << nl);
00822
00823 MINIDUMP_EXCEPTION_INFORMATION eInfo;
00824 eInfo.ThreadId = GetCurrentThreadId();
00825 eInfo.ExceptionPointers = info;
00826 eInfo.ClientPointers = FALSE;
00827
00828 LOG ("address = " << DWORD (info->ExceptionRecord->ExceptionAddress) << nl);
00829 LOG ("creating dump file...");
00830
00831 char szFile [MAX_PATH]; char szName [100];
00832 wsprintf (szName, "fdm%d.dmp", PRG_BUILD_NUMBER);
00833 GetTempPath (MAX_PATH, szFile);
00834 lstrcat (szFile, szName);
00835 HANDLE hFile = CreateFile (szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
00836 0, NULL);
00837
00838 pfnMiniDumpWriteDump(
00839 GetCurrentProcess(),
00840 GetCurrentProcessId(),
00841 hFile,
00842 MiniDumpNormal,
00843 &eInfo,
00844 NULL,
00845 NULL);
00846
00847 CloseHandle (hFile);
00848
00849 LOG ("done." << nl);
00850 LOG ("========================================================" << nl);
00851
00852 CUEDlg dlg;
00853 dlg.DoModal ();
00854
00855 return EXCEPTION_EXECUTE_HANDLER;
00856 }
00857 #include "FDM.h"
00858 #include "FDMDownloadsStat.h"
00859 #include "FDMDownload.h"
00860 #include "FDMUploader.h"
00861 #include "FDMUploadPackage.h"
00862
00863 BOOL CFdmApp::Is_Starting()
00864 {
00865 return m_bStarting;
00866 }
00867
00868 void CFdmApp::CheckLocked()
00869 {
00870 DWORD dwRes;
00871
00872 do
00873 {
00874 HANDLE hMx = CreateMutex (NULL, TRUE, "_mx_FDM_Lock_Start_");
00875 dwRes = GetLastError ();
00876 CloseHandle (hMx);
00877
00878 if (dwRes == ERROR_ALREADY_EXISTS)
00879 Sleep (100);
00880 }
00881 while (dwRes == ERROR_ALREADY_EXISTS);
00882 }
00883
00884 void CFdmApp::UninstallCustomizations()
00885 {
00886 CRegKey key;
00887 key.Open (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Download Manager");
00888
00889 char sz [MY_MAX_PATH];
00890 DWORD dw = MY_MAX_PATH;
00891
00892 key.QueryValue (sz, "CLSM_File", &dw);
00893 DeleteFile (sz);
00894
00895 dw = MY_MAX_PATH;
00896 key.QueryValue (sz, "CLFM_File", &dw);
00897 DeleteFile (sz);
00898
00899 char szIEOP [10000] = "about:blank";
00900 char szIECP [10000] = "about:blank";
00901
00902 CRegKey fdmkey;
00903 fdmkey.Open (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Download Manager");
00904
00905 dw = sizeof (szIECP);
00906 fdmkey.QueryValue (szIECP, "CustSite", &dw);
00907 dw = sizeof (szIEOP);
00908 if (ERROR_SUCCESS == fdmkey.QueryValue (szIEOP, "CIEOP", &dw))
00909 {
00910 key.Open (HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Explorer\\Main");
00911
00912 char sz2 [10000] = "about:blank";
00913 dw = sizeof (sz2);
00914 key.QueryValue (sz2, "Start Page", &dw);
00915
00916 if (lstrcmp (sz2, szIECP) == 0)
00917 key.SetValue (szIEOP, "Start Page");
00918 }
00919 }
00920
00921 BOOL CFdmApp::RegisterServer(BOOL bGlobal)
00922 {
00923 if (_App.ModifyIEUserAgent ())
00924 {
00925 fsIEUserAgent ua;
00926 ua.SetPP (IE_USERAGENT_ADDITION);
00927 ua.RemovePP ("Open Download Manager");
00928 }
00929
00930 if (bGlobal)
00931 {
00932 CRegKey key;
00933
00934 if (ERROR_SUCCESS == key.Open (HKEY_LOCAL_MACHINE, "Software\\OpenDownloadManager.ORG\\Open Download Manager", KEY_WRITE))
00935 vmsSHCopyKey (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Download Manager", key);
00936
00937 _Module.UpdateRegistryFromResource(IDR_FDM, TRUE);
00938 _Module.RegisterServer(TRUE);
00939
00940 ITypeLib *pLib = NULL;
00941
00942
00943 if (SUCCEEDED (LoadTypeLibEx (L"odm.tlb", REGKIND_REGISTER, &pLib)))
00944 pLib->Release ();
00945 else
00946 MessageBox (0, "Failed to load Open Download Manager type lib","Error",0);
00947
00948 if (_AppMgr.IsBtInstalled ())
00949 {
00950
00951 if (vmsTorrentExtension::IsAssociatedWithUs () == FALSE)
00952 {
00953 if (_App.Bittorrent_Enable () || vmsTorrentExtension::IsAssociationExist () == FALSE)
00954 {
00955 _App.Bittorrent_OldTorrentAssociation (vmsTorrentExtension::GetCurrentAssociation ());
00956 vmsTorrentExtension::Associate ();
00957 }
00958 }
00959 }
00960 }
00961
00962 WriteProfileInt ("", "cverID", 0);
00963
00964 if (GetProfileInt ("Settings\\Monitor", "IECMInited", 0) == 0)
00965 WriteProfileInt ("Settings\\Monitor", "IECMInited", TRUE);
00966 _IECMM.DeleteIEMenus ();
00967 fsIEContextMenuMgr::DeleteAllFDMsIEMenus ();
00968 _IECMM.AddIEMenus ();
00969
00970 DWORD dwMUSO = _App.Monitor_UserSwitchedOn ();
00971
00972 _IECatchMgr.ActivateIE2 ((dwMUSO & MONITOR_USERSWITCHEDON_IE) != 0);
00973
00974 vmsFirefoxMonitoring::Install (true);
00975 if (vmsFirefoxMonitoring::IsInstalled () == false)
00976 _App.Monitor_Firefox (FALSE);
00977 else
00978 _App.Monitor_Firefox ((dwMUSO & MONITOR_USERSWITCHEDON_FIREFOX) != 0);
00979
00980 _NOMgr.DeinstallFirefoxPlugin ();
00981
00982 if (dwMUSO & MONITOR_USERSWITCHEDON_NETSCAPE)
00983 _NOMgr.InstallNetscapePlugin ();
00984 if (dwMUSO & MONITOR_USERSWITCHEDON_OPERA)
00985 _NOMgr.InstallOperaPlugin ();
00986 if (dwMUSO & MONITOR_USERSWITCHEDON_SEAMONKEY)
00987 _NOMgr.InstallMozillaSuitePlugin ();
00988
00989 CRegKey key;
00990 if (ERROR_SUCCESS != key.Create (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Upload Manager"))
00991 key.Open (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Upload Manager");
00992 key.SetValue (fsGetAppDataFolder (), "force_data_folder");
00993
00994 return TRUE;
00995 }
00996
00997 void CFdmApp::Install_RegisterServer()
00998 {
00999 if (m_bATLInited == FALSE)
01000 {
01001 m_bATLInited = TRUE;
01002 _Module.Init(ObjectMap, AfxGetInstanceHandle());
01003 _Module.dwThreadID = GetCurrentThreadId();
01004 }
01005 RegisterServer (TRUE);
01006 }
01007
01008 void CFdmApp::Install_UnregisterServer()
01009 {
01010 if (m_bATLInited == FALSE)
01011 {
01012 m_bATLInited = TRUE;
01013 _Module.Init(ObjectMap, AfxGetInstanceHandle());
01014 _Module.dwThreadID = GetCurrentThreadId();
01015 }
01016
01017 _Module.UpdateRegistryFromResource(IDR_FDM, FALSE);
01018 _Module.UnregisterServer(TRUE);
01019 UnRegisterTypeLib (LIBID_FdmLib, 0, 0, LOCALE_SYSTEM_DEFAULT, SYS_WIN32);
01020
01021 if (_IECMM.IsIEMenusPresent ())
01022 WriteProfileInt ("Settings\\Monitor", "IECMInited", 0);
01023
01024 fsIEContextMenuMgr::DeleteAllFDMsIEMenus ();
01025
01026 _NOMgr.DeinstallNetscapePlugin ();
01027 _NOMgr.DeinstallOperaPlugin ();
01028 _NOMgr.DeinstallMozillaSuitePlugin ();
01029 _IECatchMgr.ActivateIE2 (FALSE);
01030 fsIEUserAgent ua;
01031 ua.RemovePP (IE_USERAGENT_ADDITION);
01032 UninstallCustomizations ();
01033 vmsFirefoxMonitoring::Install (false);
01034
01035 if (vmsTorrentExtension::IsAssociatedWithUs ())
01036 vmsTorrentExtension::AssociateWith (_App.Bittorrent_OldTorrentAssociation ());
01037 }
01038
01039 void CFdmApp::SaveSettings()
01040 {
01041 if (IS_PORTABLE_MODE)
01042 {
01043
01044 vmsAppSettingsStore* pStgs = _App.get_SettingsStore ();
01045 CString strStgsFile = fsGetDataFilePath ("settings.dat");
01046 pStgs->SaveSettingsToFile (strStgsFile);
01047 }
01048 }
01049
01050 void CFdmApp::IntegrationSettings()
01051 {
01052 vmsUploadsDllCaller udc;
01053 HMODULE hUploadsDll;
01054
01055 #ifndef _DEBUG
01056 CString strFP = fsGetFumProgramFilesFolder ();
01057 hUploadsDll = LoadLibrary (strFP + "fumcore.dll");
01058 #else
01059 hUploadsDll = LoadLibrary ("E:\\VCW\\FDM\\FDM\\Uploader\\CoreDll\\Debug\\fumcore.dll");
01060 ASSERT (hUploadsDll != NULL);
01061 #endif
01062
01063 if (hUploadsDll == NULL)
01064 return;
01065
01066 vmsUploadsDll::FNSDC pfnSetCaller = (vmsUploadsDll::FNSDC) GetProcAddress (hUploadsDll,
01067 "_SetDllCaller");
01068 ASSERT (pfnSetCaller != NULL);
01069 pfnSetCaller (&udc);
01070
01071 if (lstrcmpi (m_lpCmdLine, "-suis") == 0)
01072 {
01073 vmsUploadsDll::FNSIS pfnShowIntegrationSettings = (vmsUploadsDll::FNSIS) GetProcAddress (hUploadsDll,
01074 "_ShowIntegrationSettings");
01075 ASSERT (pfnShowIntegrationSettings != NULL);
01076 pfnShowIntegrationSettings (NULL);
01077 }
01078 else
01079 {
01080 vmsUploadsDll::FNEI pfnEnableIntegration = (vmsUploadsDll::FNEI) GetProcAddress (hUploadsDll,
01081 "_EnableIntegration");
01082 ASSERT (pfnEnableIntegration != NULL);
01083 pfnEnableIntegration (lstrcmpi (m_lpCmdLine, "-euis") == 0, 0);
01084 }
01085
01086 FreeLibrary (hUploadsDll);
01087 }
01088
01089 DWORD WINAPI CFdmApp::_threadExitProcess(LPVOID lp)
01090 {
01091 LOG ("DSA::tEP:sleep " << (DWORD)lp << " seconds" << nl);
01092 Sleep (((DWORD)lp) * 1000);
01093 ASSERT (FALSE);
01094 LOG ("DSA::tEP:terminating" << nl);
01095 HANDLE hProcess = OpenProcess (PROCESS_TERMINATE, FALSE, GetCurrentProcessId ());
01096 TerminateProcess (hProcess, (DWORD)-1);
01097 LOG ("DSA::tEP:unexpected error " << GetLastError () << nl);
01098 return 0;
01099 }
01100
01101 void CFdmApp::ScheduleExitProcess(DWORD dwSeconds)
01102 {
01103 DWORD dw;
01104 CloseHandle (
01105 ::CreateThread (NULL, 0, _threadExitProcess, (LPVOID)dwSeconds, 0, &dw));
01106 }
01107
01108 void CFdmApp::CheckRegistry()
01109 {
01110 fsString str = "%56%69%63%4D%61%6E%20%53%6F%66%74%77%61%72%65";
01111 fsDecodeHtmlUrl (str);
01112 CString strOldKey = "Software\\"; strOldKey += str;
01113 CString strOldRKey = strOldKey;
01114 strOldKey += "\\Open Download Manager";
01115
01116
01117 CRegKey key;
01118 if (ERROR_SUCCESS == key.Open (HKEY_CURRENT_USER, strOldKey))
01119 {
01120 LOG ("old key detected." << nl);
01121
01122 CRegKey key2;
01123 if (ERROR_SUCCESS != key2.Open (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG\\Open Download Manager\\Settings\\View"))
01124 {
01125 LOG ("importing old key" << nl);
01126
01127 if (ERROR_SUCCESS == key2.Create (HKEY_CURRENT_USER, "Software\\OpenDownloadManager.ORG"))
01128 {
01129 key.Open (HKEY_CURRENT_USER, strOldRKey);
01130
01131 CString strPath = GetProfileString ("", "Path", "");
01132 LONG nRes = fsCopyKey (key, key2, "Open Download Manager", "Open Download Manager");
01133 nRes;
01134 WriteProfileString ("", "Path", strPath);
01135
01136 key.RecurseDeleteKey ("Open Download Manager");
01137
01138 LOG ("finished importing old key. result = " << nRes << nl);
01139 }
01140 else
01141 {
01142 LOG ("failed to create a new key" << nl);
01143 }
01144 }
01145 else
01146 {
01147 LOG ("importing of old key cancelled." << nl);
01148 }
01149 }
01150 }
01151
01152 AFX_MODULE_STATE* CFdmApp::GetModuleState()
01153 {
01154 return m_pModuleState;
01155 }