WinMain.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <CoreMain.h>
  5. #include <Log.h>
  6. #include <TextsWin.h>
  7. #include <TextsCore.h>
  8. #include <HelpWin.h>
  9. #include "CustomScpExplorer.h"
  10. #include "TerminalManager.h"
  11. #include "NonVisual.h"
  12. #include "Glyphs.h"
  13. #include "ProgParams.h"
  14. #include "Setup.h"
  15. #include "WinConfiguration.h"
  16. #include "GUITools.h"
  17. #include "Tools.h"
  18. #include "WinApi.h"
  19. //---------------------------------------------------------------------------
  20. #pragma package(smart_init)
  21. //---------------------------------------------------------------------------
  22. void __fastcall GetLoginData(UnicodeString SessionName, TOptions * Options,
  23. TObjectList * DataList, UnicodeString & DownloadFile)
  24. {
  25. bool DefaultsOnly = false;
  26. bool Close = false;
  27. if (StoredSessions->IsFolder(SessionName) ||
  28. StoredSessions->IsWorkspace(SessionName))
  29. {
  30. StoredSessions->GetFolderOrWorkspace(SessionName, DataList);
  31. }
  32. else
  33. {
  34. TSessionData * SessionData =
  35. StoredSessions->ParseUrl(SessionName, Options, DefaultsOnly,
  36. &DownloadFile);
  37. DataList->Add(SessionData);
  38. if (DataList->Count == 1)
  39. {
  40. TSessionData * SessionData = NOT_NULL(dynamic_cast<TSessionData *>(DataList->Items[0]));
  41. if (SessionData->SaveOnly)
  42. {
  43. Configuration->Usage->Inc(L"CommandLineSessionSave");
  44. TSessionData * SavedSession = DoSaveSession(SessionData, NULL, true, NULL);
  45. Close = (SavedSession == NULL);
  46. if (!Close)
  47. {
  48. WinConfiguration->LastStoredSession = SavedSession->Name;
  49. }
  50. DataList->Clear();
  51. }
  52. }
  53. }
  54. if (!Close)
  55. {
  56. if ((DataList->Count == 0) ||
  57. !dynamic_cast<TSessionData *>(DataList->Items[0])->CanLogin ||
  58. DefaultsOnly)
  59. {
  60. // Note that GetFolderOrWorkspace never returns sites that !CanLogin,
  61. // so we should not get here with more then one site.
  62. // Though we should be good, if we ever do.
  63. assert(DataList->Count <= 1);
  64. if (!DoLoginDialog(StoredSessions, DataList, loStartup))
  65. {
  66. DataList->Clear();
  67. }
  68. }
  69. }
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall Upload(TTerminal * Terminal, TStrings * FileList, bool UseDefaults)
  73. {
  74. UnicodeString TargetDirectory;
  75. TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
  76. TargetDirectory = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  77. int Options = coDisableQueue;
  78. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  79. if (UseDefaults ||
  80. DoCopyDialog(true, false, FileList, TargetDirectory, &CopyParam, Options,
  81. CopyParamAttrs, NULL))
  82. {
  83. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, 0);
  84. }
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall Download(TTerminal * Terminal, const UnicodeString FileName,
  88. bool UseDefaults)
  89. {
  90. UnicodeString TargetDirectory;
  91. TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
  92. TStrings * FileList = NULL;
  93. try
  94. {
  95. FileList = new TStringList();
  96. TRemoteFile * File = Terminal->Files->FindFile(FileName);
  97. if (File == NULL)
  98. {
  99. throw Exception(FMTLOAD(FILE_NOT_EXISTS, (FileName)));
  100. }
  101. FileList->AddObject(FileName, File);
  102. UnicodeString LocalDirectory = ExpandFileName(Terminal->SessionData->LocalDirectory);
  103. if (LocalDirectory.IsEmpty())
  104. {
  105. LocalDirectory = GetPersonalFolder();
  106. }
  107. TargetDirectory = IncludeTrailingBackslash(LocalDirectory);
  108. int Options = coDisableQueue;
  109. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Download;
  110. if (UseDefaults ||
  111. DoCopyDialog(false, false, FileList, TargetDirectory, &CopyParam,
  112. Options, CopyParamAttrs, NULL))
  113. {
  114. Terminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, 0);
  115. }
  116. }
  117. __finally
  118. {
  119. delete FileList;
  120. }
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall Edit(TCustomScpExplorerForm * ScpExplorer, TStrings * FileList)
  124. {
  125. ScpExplorer->StandaloneEdit(FileList->Strings[0]);
  126. Abort();
  127. }
  128. //---------------------------------------------------------------------------
  129. void __fastcall SynchronizeDirectories(TTerminal * Terminal,
  130. TStrings * CommandParams,
  131. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory)
  132. {
  133. if (CommandParams->Count >= 1)
  134. {
  135. LocalDirectory = CommandParams->Strings[0];
  136. }
  137. else if (!Terminal->SessionData->LocalDirectory.IsEmpty())
  138. {
  139. LocalDirectory = ExpandFileName(Terminal->SessionData->LocalDirectory);
  140. }
  141. else
  142. {
  143. LocalDirectory = WinConfiguration->ScpExplorer.LastLocalTargetDirectory;
  144. }
  145. if (CommandParams->Count >= 2)
  146. {
  147. RemoteDirectory = CommandParams->Strings[1];
  148. }
  149. else
  150. {
  151. RemoteDirectory = Terminal->CurrentDirectory;
  152. }
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall FullSynchronize(TTerminal * Terminal, TCustomScpExplorerForm * ScpExplorer,
  156. TStrings * CommandParams, bool UseDefaults)
  157. {
  158. UnicodeString LocalDirectory;
  159. UnicodeString RemoteDirectory;
  160. SynchronizeDirectories(Terminal, CommandParams, LocalDirectory, RemoteDirectory);
  161. bool SaveMode = true;
  162. // bit ugly
  163. TSynchronizeMode Mode = (TSynchronizeMode)GUIConfiguration->SynchronizeMode;
  164. if (ScpExplorer->DoFullSynchronizeDirectories(LocalDirectory,
  165. RemoteDirectory, Mode, SaveMode, UseDefaults))
  166. {
  167. if (SaveMode)
  168. {
  169. GUIConfiguration->SynchronizeMode = Mode;
  170. }
  171. Terminal->CloseOnCompletion();
  172. }
  173. else
  174. {
  175. Abort();
  176. }
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall Synchronize(TTerminal * Terminal, TCustomScpExplorerForm * ScpExplorer,
  180. TStrings * CommandParams, bool UseDefaults)
  181. {
  182. UnicodeString LocalDirectory;
  183. UnicodeString RemoteDirectory;
  184. SynchronizeDirectories(Terminal, CommandParams, LocalDirectory, RemoteDirectory);
  185. ScpExplorer->DoSynchronizeDirectories(LocalDirectory, RemoteDirectory, UseDefaults);
  186. Abort();
  187. }
  188. //---------------------------------------------------------------------------
  189. void __fastcall ImportSitesIfAny()
  190. {
  191. if (!WinConfiguration->AutoImportedFromPuttyOrFilezilla)
  192. {
  193. bool AnyPuttySession = GUIConfiguration->AnyPuttySessionForImport(StoredSessions);
  194. bool AnyFilezillaSession = GUIConfiguration->AnyFilezillaSessionForImport(StoredSessions);
  195. if (AnyPuttySession || AnyFilezillaSession)
  196. {
  197. UnicodeString PuttySource = LoadStrPart(IMPORT_SESSIONS2, 2);
  198. UnicodeString FilezillaSource = LoadStrPart(IMPORT_SESSIONS2, 3);
  199. UnicodeString Source;
  200. if (AnyPuttySession && AnyFilezillaSession)
  201. {
  202. Source = FORMAT(LoadStrPart(IMPORT_SESSIONS2, 4), (PuttySource, FilezillaSource));
  203. }
  204. else if (AnyPuttySession)
  205. {
  206. Source = PuttySource;
  207. }
  208. else if (AnyFilezillaSession)
  209. {
  210. Source = FilezillaSource;
  211. }
  212. else
  213. {
  214. FAIL;
  215. }
  216. UnicodeString Message = FORMAT(LoadStrPart(IMPORT_SESSIONS2, 1), (Source));
  217. if (MessageDialog(Message, qtConfirmation,
  218. qaYes | qaNo, HELP_IMPORT_SESSIONS) == qaYes)
  219. {
  220. DoImportSessionsDialog(NULL);
  221. }
  222. WinConfiguration->AutoImportedFromPuttyOrFilezilla = true;
  223. }
  224. }
  225. }
  226. //---------------------------------------------------------------------------
  227. void __fastcall Usage(UnicodeString Param)
  228. {
  229. while (!Param.IsEmpty())
  230. {
  231. UnicodeString Pair = ::CutToChar(Param, L',', true);
  232. if (!Pair.IsEmpty())
  233. {
  234. if (Pair[Pair.Length()] == L'+')
  235. {
  236. UnicodeString Key = Pair.SubString(1, Pair.Length() - 1).Trim();
  237. Configuration->Usage->Inc(Key);
  238. }
  239. else
  240. {
  241. UnicodeString Key = ::CutToChar(Pair, L':', true);
  242. Configuration->Usage->Set(Key, Pair.Trim());
  243. }
  244. }
  245. }
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall RecordWrapperVersions(UnicodeString ConsoleVersion, UnicodeString DotNetVersion)
  249. {
  250. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  251. if (!DotNetVersion.IsEmpty())
  252. {
  253. Updates.DotNetVersion = DotNetVersion;
  254. }
  255. if (!ConsoleVersion.IsEmpty())
  256. {
  257. Updates.ConsoleVersion = ConsoleVersion;
  258. }
  259. WinConfiguration->Updates = Updates;
  260. if (Configuration->Storage == stNul)
  261. {
  262. Configuration->SetDefaultStorage();
  263. try
  264. {
  265. THierarchicalStorage * Storage = Configuration->CreateConfigStorage();
  266. try
  267. {
  268. Storage->AccessMode = smReadWrite;
  269. if (Storage->OpenSubKey(Configuration->ConfigurationSubKey, true) &&
  270. Storage->OpenSubKey(L"Interface\\Updates", true, true))
  271. {
  272. if (!DotNetVersion.IsEmpty())
  273. {
  274. Storage->WriteString(L"DotNetVersion", DotNetVersion);
  275. }
  276. if (!ConsoleVersion.IsEmpty())
  277. {
  278. Storage->WriteString(L"ConsoleVersion", ConsoleVersion);
  279. }
  280. }
  281. }
  282. __finally
  283. {
  284. delete Storage;
  285. }
  286. }
  287. __finally
  288. {
  289. Configuration->SetNulStorage();
  290. }
  291. }
  292. }
  293. //---------------------------------------------------------------------------
  294. static UnicodeString ColorToRGBStr(TColor Color)
  295. {
  296. int RGB = ColorToRGB(Color);
  297. int R = GetRValue(RGB);
  298. int G = GetGValue(RGB);
  299. int B = GetBValue(RGB);
  300. UnicodeString Result = FORMAT(L"%.2x%.2x%.2x", (R, G, B));
  301. return Result;
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall UpdateStaticUsage()
  305. {
  306. Configuration->Usage->Inc(L"Runs");
  307. Configuration->Usage->UpdateCurrentVersion();
  308. UnicodeString WindowsVersion = FORMAT("%d.%d.%d %s", (Win32MajorVersion, Win32MinorVersion, Win32BuildNumber, Win32CSDVersion));
  309. Configuration->Usage->Set(L"WindowsVersion", (WindowsVersion));
  310. Configuration->Usage->Set(L"WindowsProductName", (WindowsProductName()));
  311. DWORD Type;
  312. GetWindowsProductType(Type);
  313. Configuration->Usage->Set(L"WindowsProductType", (static_cast<int>(Type)));
  314. Configuration->Usage->Set(L"Windows64", IsWin64());
  315. Configuration->Usage->Set(L"DefaultLocale",
  316. IntToHex(static_cast<int>(GetDefaultLCID()), 4));
  317. Configuration->Usage->Set(L"Locale",
  318. IntToHex(static_cast<int>(WinConfiguration->Locale), 4));
  319. Configuration->Usage->Set(L"PixelsPerInch", Screen->PixelsPerInch);
  320. bool PixelsPerInchSystemDiffers = false;
  321. bool PixelsPerInchMonitorsDiffer = false;
  322. bool PixelsPerInchAxesDiffer = false;
  323. HINSTANCE ShCoreLibrary = LoadLibrary(L"shcore.dll");
  324. if (ShCoreLibrary != NULL)
  325. {
  326. GetDpiForMonitorProc GetDpiForMonitor =
  327. (GetDpiForMonitorProc)GetProcAddress(ShCoreLibrary, "GetDpiForMonitor");
  328. if (GetDpiForMonitor != NULL)
  329. {
  330. unsigned int PrimaryDpiX;
  331. unsigned int PrimaryDpiY;
  332. for (int Index = 0; Index < Screen->MonitorCount; Index++)
  333. {
  334. unsigned int DpiX;
  335. unsigned int DpiY;
  336. GetDpiForMonitor(Screen->Monitors[Index]->Handle, MDT_Default, &DpiX, &DpiY);
  337. if (DpiX != DpiY)
  338. {
  339. PixelsPerInchAxesDiffer = true;
  340. }
  341. if (Index == 0)
  342. {
  343. PrimaryDpiX = DpiX;
  344. PrimaryDpiY = DpiY;
  345. // PixelsPerInch is GetDeviceCaps(DC, LOGPIXELSY)
  346. if (DpiY != (unsigned int)Screen->PixelsPerInch)
  347. {
  348. PixelsPerInchSystemDiffers = true;
  349. }
  350. }
  351. else
  352. {
  353. if ((DpiX != PrimaryDpiX) ||
  354. (DpiY != PrimaryDpiY))
  355. {
  356. PixelsPerInchMonitorsDiffer = true;
  357. }
  358. }
  359. }
  360. }
  361. }
  362. if (PixelsPerInchSystemDiffers)
  363. {
  364. Configuration->Usage->Inc(L"PixelsPerInchSystemDiffered");
  365. }
  366. Configuration->Usage->Set(L"PixelsPerInchMonitorsDiffer", PixelsPerInchMonitorsDiffer);
  367. Configuration->Usage->Set(L"PixelsPerInchAxesDiffer", PixelsPerInchAxesDiffer);
  368. Configuration->Usage->Set(L"WorkAreaWidth", Screen->WorkAreaWidth);
  369. Configuration->Usage->Set(L"WorkAreaHeight", Screen->WorkAreaHeight);
  370. HDC DC = GetDC(NULL);
  371. int Planes = GetDeviceCaps(DC, PLANES);
  372. int BitsPixel = GetDeviceCaps(DC, BITSPIXEL);
  373. Configuration->Usage->Set(L"ColorDepth", Planes * BitsPixel);
  374. Configuration->Usage->Set(L"MonitorCount", Screen->MonitorCount);
  375. Configuration->Usage->Set(L"NotUseThemes", !UseThemes());
  376. Configuration->Usage->Set(L"ThemeDefaultFontSize", Application->DefaultFont->Size);
  377. Configuration->Usage->Set(L"ThemeIconFontSize", Screen->IconFont->Size);
  378. Configuration->Usage->Set(L"SysColorWindow", ColorToRGBStr(clWindow));
  379. Configuration->Usage->Set(L"SysColorBtnFace", ColorToRGBStr(clBtnFace));
  380. Configuration->Usage->Set(L"SysColorWindowText", ColorToRGBStr(clWindowText));
  381. UnicodeString ProgramsFolder;
  382. ::SpecialFolderLocation(CSIDL_PROGRAM_FILES, ProgramsFolder);
  383. ProgramsFolder = IncludeTrailingBackslash(ExpandFileName(ProgramsFolder));
  384. UnicodeString ExeName = ExpandFileName(Application->ExeName);
  385. bool InProgramFiles = AnsiSameText(ExeName.SubString(1, ProgramsFolder.Length()), ProgramsFolder);
  386. Configuration->Usage->Set(L"InProgramFiles", InProgramFiles);
  387. Configuration->Usage->Set(L"IsInstalled", IsInstalled());
  388. Configuration->Usage->Set(L"Wine", IsWine());
  389. WinConfiguration->UpdateStaticUsage();
  390. }
  391. //---------------------------------------------------------------------------
  392. void __fastcall MaintenanceTask()
  393. {
  394. CoreMaintenanceTask();
  395. }
  396. //---------------------------------------------------------------------------
  397. struct TFindProcessMainWindowParam
  398. {
  399. unsigned long ProcessId;
  400. HWND HiddenWindow;
  401. HWND MainWindow;
  402. };
  403. //---------------------------------------------------------------------------
  404. BOOL __stdcall FindProcessMainWindow(HWND Handle, LPARAM AParam)
  405. {
  406. TFindProcessMainWindowParam & Param = *reinterpret_cast<TFindProcessMainWindowParam *>(AParam);
  407. unsigned long ProcessId;
  408. if ((Handle != Param.HiddenWindow) &&
  409. (Param.MainWindow == 0) && // optimization
  410. (GetWindowThreadProcessId(Handle, &ProcessId) != 0) &&
  411. (ProcessId == Param.ProcessId))
  412. {
  413. TCopyDataMessage Message;
  414. Message.Version = TCopyDataMessage::Version1;
  415. COPYDATASTRUCT CopyData;
  416. CopyData.cbData = sizeof(Message);
  417. CopyData.lpData = &Message;
  418. Message.Command = TCopyDataMessage::CommandCanCommandLine;
  419. LRESULT SendResult =
  420. SendMessage(Handle, WM_COPYDATA, reinterpret_cast<WPARAM>(HInstance),
  421. reinterpret_cast<LPARAM>(&CopyData));
  422. if (SendResult > 0)
  423. {
  424. Param.MainWindow = Handle;
  425. }
  426. }
  427. return TRUE;
  428. }
  429. //---------------------------------------------------------------------------
  430. bool __fastcall SendToAnotherInstance()
  431. {
  432. HWND HiddenWindow = FindWindow(HIDDEN_WINDOW_NAME, NULL);
  433. bool Result = (HiddenWindow != NULL);
  434. if (Result)
  435. {
  436. TCopyDataMessage Message;
  437. Message.Version = TCopyDataMessage::Version1;
  438. COPYDATASTRUCT CopyData;
  439. CopyData.cbData = sizeof(Message);
  440. CopyData.lpData = &Message;
  441. // this test is actually redundant, it just a kind of optimization to avoid expensive
  442. // EnumWindows, we can achieve the same by testing FindProcessMainWindowParam.MainWindow,
  443. // before sending CommandCommandLine
  444. Message.Command = TCopyDataMessage::CommandCanCommandLine;
  445. LRESULT SendResult =
  446. SendMessage(HiddenWindow, WM_COPYDATA, reinterpret_cast<WPARAM>(HInstance),
  447. reinterpret_cast<LPARAM>(&CopyData));
  448. Result = (SendResult > 0);
  449. if (Result)
  450. {
  451. TFindProcessMainWindowParam FindProcessMainWindowParam;
  452. if (GetWindowThreadProcessId(HiddenWindow, &FindProcessMainWindowParam.ProcessId) != 0)
  453. {
  454. FindProcessMainWindowParam.HiddenWindow = HiddenWindow;
  455. FindProcessMainWindowParam.MainWindow = 0;
  456. if (EnumWindows(FindProcessMainWindow, reinterpret_cast<LPARAM>(&FindProcessMainWindowParam)) &&
  457. (FindProcessMainWindowParam.MainWindow != 0))
  458. {
  459. // Restore window, if minimized
  460. ShowWindow(FindProcessMainWindowParam.MainWindow, SW_RESTORE);
  461. // bring it to foreground
  462. SetForegroundWindow(FindProcessMainWindowParam.MainWindow);
  463. }
  464. }
  465. Message.Command = TCopyDataMessage::CommandCommandLine;
  466. wcsncpy(Message.CommandLine, CmdLine, LENOF(Message.CommandLine));
  467. NULL_TERMINATE(Message.CommandLine);
  468. LRESULT SendResult =
  469. SendMessage(HiddenWindow, WM_COPYDATA,
  470. reinterpret_cast<WPARAM>(HInstance), reinterpret_cast<LPARAM>(&CopyData));
  471. Result = (SendResult > 0);
  472. }
  473. }
  474. return Result;
  475. }
  476. //---------------------------------------------------------------------------
  477. bool __fastcall ShowUpdatesIfAvailable()
  478. {
  479. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  480. int CurrentCompoundVer = Configuration->CompoundVersion;
  481. bool Result =
  482. Updates.ShowOnStartup &&
  483. Updates.HaveValidResultsForVersion(CurrentCompoundVer) &&
  484. !Updates.Results.Disabled &&
  485. ((Updates.Results.Version > CurrentCompoundVer) || !Updates.Results.Message.IsEmpty()) &&
  486. !Updates.ShownResults;
  487. if (Result)
  488. {
  489. Configuration->Usage->Inc(L"UpdateStartup");
  490. Result = CheckForUpdates(true);
  491. if (Result)
  492. {
  493. Configuration->Usage->Inc(L"UpdateDownloadOpensStartup");
  494. }
  495. }
  496. return Result;
  497. }
  498. //---------------------------------------------------------------------------
  499. int __fastcall Execute()
  500. {
  501. assert(StoredSessions);
  502. TProgramParams * Params = TProgramParams::Instance();
  503. assert(Params);
  504. // do not flash message boxes on startup
  505. SetOnForeground(true);
  506. // let installer know, that some instance of application is running
  507. CreateMutex(NULL, False, AppName.c_str());
  508. bool OnlyInstance = (GetLastError() == 0);
  509. UpdateStaticUsage();
  510. UnicodeString KeyFile;
  511. if (Params->FindSwitch(L"PrivateKey", KeyFile))
  512. {
  513. WinConfiguration->DefaultKeyFile = KeyFile;
  514. }
  515. UnicodeString ConsoleVersion;
  516. UnicodeString DotNetVersion;
  517. Params->FindSwitch(L"Console", ConsoleVersion);
  518. Params->FindSwitch(L"DotNet", DotNetVersion);
  519. if (!ConsoleVersion.IsEmpty() || !DotNetVersion.IsEmpty())
  520. {
  521. RecordWrapperVersions(ConsoleVersion, DotNetVersion);
  522. }
  523. if (!DotNetVersion.IsEmpty())
  524. {
  525. Configuration->Usage->Inc(L"ConsoleDotNet");
  526. }
  527. UnicodeString SwitchValue;
  528. if (Params->FindSwitch(L"loglevel", SwitchValue))
  529. {
  530. int StarPos = SwitchValue.Pos(L"*");
  531. if (StarPos > 0)
  532. {
  533. bool LogSensitive = true;
  534. SwitchValue.Delete(StarPos, 1);
  535. if ((StarPos <= SwitchValue.Length()) &&
  536. (SwitchValue[StarPos] == L'-'))
  537. {
  538. LogSensitive = false;
  539. SwitchValue.Delete(StarPos, 1);
  540. }
  541. SwitchValue = SwitchValue.Trim();
  542. Configuration->TemporaryLogSensitive(LogSensitive);
  543. }
  544. int LogProtocol;
  545. if (!SwitchValue.IsEmpty() && TryStrToInt(SwitchValue, LogProtocol) && (LogProtocol >= 0))
  546. {
  547. Configuration->TemporaryLogProtocol(LogProtocol);
  548. }
  549. }
  550. TConsoleMode Mode = cmNone;
  551. if (Params->FindSwitch(L"help") || Params->FindSwitch(L"h") || Params->FindSwitch(L"?"))
  552. {
  553. Mode = cmHelp;
  554. }
  555. else if (Params->FindSwitch(L"batchsettings"))
  556. {
  557. Mode = cmBatchSettings;
  558. }
  559. // We have to check for /console only after the other options,
  560. // as the /console is always used when we are run by winscp.com
  561. // (ambiguous use to pass console version)
  562. else if (Params->FindSwitch(L"Console") || Params->FindSwitch(L"script") ||
  563. Params->FindSwitch(L"command"))
  564. {
  565. Mode = cmScripting;
  566. }
  567. if (Mode != cmNone)
  568. {
  569. return Console(Mode);
  570. }
  571. TTerminalManager * TerminalManager = NULL;
  572. GlyphsModule = NULL;
  573. NonVisualDataModule = NULL;
  574. TStrings * CommandParams = new TStringList;
  575. try
  576. {
  577. TerminalManager = TTerminalManager::Instance();
  578. HANDLE ResourceModule = GUIConfiguration->ChangeResourceModule(NULL);
  579. try
  580. {
  581. GlyphsModule = new TGlyphsModule(Application);
  582. }
  583. __finally
  584. {
  585. GUIConfiguration->ChangeResourceModule(ResourceModule);
  586. }
  587. NonVisualDataModule = new TNonVisualDataModule(Application);
  588. LogForm = NULL;
  589. Application->HintHidePause = 3000;
  590. UnicodeString IniFileName = Params->SwitchValue(L"ini");
  591. if (!IniFileName.IsEmpty())
  592. {
  593. UnicodeString IniFileNameExpanded = ExpandEnvironmentVariables(IniFileName);
  594. if (!FileExists(::ApiPath(IniFileNameExpanded)))
  595. {
  596. // this should be displayed rather at the very beginning.
  597. // however for simplicity (GUI-only), we do it only here.
  598. MessageDialog(FMTLOAD(FILE_NOT_EXISTS, (IniFileNameExpanded)), qtError, qaOK);
  599. }
  600. }
  601. if (Params->FindSwitch(L"UninstallCleanup"))
  602. {
  603. MaintenanceTask();
  604. // The innosetup cannot skip UninstallCleanup run task for silent uninstalls,
  605. // workaround is that we create mutex in uninstaller, if it runs silent, and
  606. // ignore the UninstallCleanup, when the mutex exists.
  607. if ((OpenMutex(SYNCHRONIZE, false, L"WinSCPSilentUninstall") == NULL) &&
  608. (MessageDialog(MainInstructions(LoadStr(UNINSTALL_CLEANUP)), qtConfirmation,
  609. qaYes | qaNo, HELP_UNINSTALL_CLEANUP) == qaYes))
  610. {
  611. DoCleanupDialog(StoredSessions, Configuration);
  612. }
  613. }
  614. else if (Params->FindSwitch(L"RegisterForDefaultProtocols") ||
  615. Params->FindSwitch(L"RegisterAsUrlHandler")) // BACKWARD COMPATIBILITY
  616. {
  617. MaintenanceTask();
  618. if (CheckSafe(Params))
  619. {
  620. RegisterForDefaultProtocols();
  621. Configuration->DontSave();
  622. }
  623. }
  624. else if (Params->FindSwitch(L"UnregisterForProtocols"))
  625. {
  626. MaintenanceTask();
  627. if (CheckSafe(Params))
  628. {
  629. UnregisterForProtocols();
  630. Configuration->DontSave();
  631. }
  632. }
  633. else if (Params->FindSwitch(L"AddSearchPath"))
  634. {
  635. MaintenanceTask();
  636. if (CheckSafe(Params))
  637. {
  638. AddSearchPath(ExtractFilePath(Application->ExeName));
  639. Configuration->DontSave();
  640. }
  641. }
  642. else if (Params->FindSwitch(L"RemoveSearchPath"))
  643. {
  644. MaintenanceTask();
  645. if (CheckSafe(Params))
  646. {
  647. try
  648. {
  649. RemoveSearchPath(ExtractFilePath(Application->ExeName));
  650. }
  651. catch(...)
  652. {
  653. // ignore errors
  654. // (RemoveSearchPath is called always on uninstallation,
  655. // even if AddSearchPath was not used, so we would get the error
  656. // always for non-priviledged user)
  657. }
  658. Configuration->DontSave();
  659. }
  660. }
  661. else if (Params->FindSwitch(L"ImportSitesIfAny"))
  662. {
  663. MaintenanceTask();
  664. ImportSitesIfAny();
  665. }
  666. else if (Params->FindSwitch(L"Usage", SwitchValue))
  667. {
  668. MaintenanceTask();
  669. Usage(SwitchValue);
  670. }
  671. else if (Params->FindSwitch(L"Update"))
  672. {
  673. MaintenanceTask();
  674. CheckForUpdates(false);
  675. }
  676. else if (ShowUpdatesIfAvailable())
  677. {
  678. // noop
  679. }
  680. else if (Params->FindSwitch(L"Exit"))
  681. {
  682. // noop
  683. MaintenanceTask();
  684. Configuration->DontSave();
  685. }
  686. else if (Params->FindSwitch(L"MaintenanceTask"))
  687. {
  688. // Parameter /MaintenanceTask can be added to command-line when executing maintenance tasks
  689. // (e.g. from installer) just in case old version of WinSCP is called by mistake
  690. MaintenanceTask();
  691. Configuration->DontSave();
  692. }
  693. else
  694. {
  695. enum { pcNone, pcUpload, pcFullSynchronize, pcSynchronize, pcEdit } ParamCommand;
  696. ParamCommand = pcNone;
  697. UnicodeString AutoStartSession;
  698. UnicodeString DownloadFile;
  699. bool UseDefaults = false;
  700. // do not check for temp dirs for service tasks (like RegisterAsUrlHandler)
  701. if (OnlyInstance &&
  702. WinConfiguration->TemporaryDirectoryCleanup)
  703. {
  704. TemporaryDirectoryCleanup();
  705. }
  706. WinConfiguration->CheckDefaultTranslation();
  707. if (!Params->Empty)
  708. {
  709. if (Params->FindSwitch(L"Defaults") && CheckSafe(Params))
  710. {
  711. UseDefaults = true;
  712. }
  713. if (Params->FindSwitch(UPLOAD_SWITCH, CommandParams))
  714. {
  715. ParamCommand = pcUpload;
  716. if (CommandParams->Count == 0)
  717. {
  718. throw Exception(NO_UPLOAD_LIST_ERROR);
  719. }
  720. }
  721. if (Params->FindSwitch(UPLOAD_IF_ANY_SWITCH, CommandParams))
  722. {
  723. if (CommandParams->Count > 0)
  724. {
  725. ParamCommand = pcUpload;
  726. }
  727. }
  728. else if (Params->FindSwitch(L"Synchronize", CommandParams, 2))
  729. {
  730. ParamCommand = pcFullSynchronize;
  731. }
  732. else if (Params->FindSwitch(L"KeepUpToDate", CommandParams, 2))
  733. {
  734. ParamCommand = pcSynchronize;
  735. }
  736. else if (Params->FindSwitch(L"Edit", CommandParams, 1) &&
  737. (CommandParams->Count == 1))
  738. {
  739. ParamCommand = pcEdit;
  740. }
  741. }
  742. if (Params->ParamCount > 0)
  743. {
  744. if ((ParamCommand == pcNone) &&
  745. (WinConfiguration->ExternalSessionInExistingInstance != OpenInNewWindow()) &&
  746. !Params->FindSwitch(NEWINSTANCE_SWICH) &&
  747. SendToAnotherInstance())
  748. {
  749. Configuration->Usage->Inc(L"SendToAnotherInstance");
  750. return 0;
  751. }
  752. AutoStartSession = Params->Param[1];
  753. Params->ParamsProcessed(1, 1);
  754. UnicodeString CounterName;
  755. if (Params->FindSwitch(JUMPLIST_SWITCH))
  756. {
  757. CounterName = L"CommandLineJumpList";
  758. }
  759. else if (Params->FindSwitch(DESKTOP_SWITCH))
  760. {
  761. CounterName = L"CommandLineDesktop";
  762. }
  763. else if (Params->FindSwitch(SEND_TO_HOOK_SWITCH))
  764. {
  765. CounterName = L"CommandLineSendToHook";
  766. }
  767. else
  768. {
  769. CounterName = L"CommandLineSession2";
  770. }
  771. Configuration->Usage->Inc(CounterName);
  772. }
  773. else if (WinConfiguration->EmbeddedSessions && StoredSessions->Count)
  774. {
  775. AutoStartSession = StoredSessions->Sessions[0]->Name;
  776. }
  777. else
  778. {
  779. AutoStartSession = WinConfiguration->AutoStartSession;
  780. }
  781. // from now flash message boxes on background
  782. SetOnForeground(false);
  783. bool Retry;
  784. do
  785. {
  786. Retry = false;
  787. TObjectList * DataList = new TObjectList();
  788. GetLoginData(AutoStartSession, Params, DataList, DownloadFile);
  789. // from now on, we do not support runtime locale change
  790. GUIConfiguration->CanApplyLocaleImmediately = false;
  791. try
  792. {
  793. if (DataList->Count > 0)
  794. {
  795. if (CheckSafe(Params))
  796. {
  797. UnicodeString LogFile;
  798. if (Params->FindSwitch(L"Log", LogFile))
  799. {
  800. Configuration->TemporaryLogging(LogFile);
  801. }
  802. if (Params->FindSwitch(L"XmlLog", LogFile))
  803. {
  804. Configuration->TemporaryActionsLogging(LogFile);
  805. }
  806. }
  807. try
  808. {
  809. assert(!TerminalManager->ActiveTerminal);
  810. TerminalManager->ActiveTerminal =
  811. TerminalManager->NewTerminals(DataList);
  812. if (TerminalManager->Count == 0)
  813. {
  814. // do not prompt with login dialog, if connection of
  815. // auto-start session (typically from command line) failed
  816. if (AutoStartSession.IsEmpty())
  817. {
  818. Retry = true;
  819. }
  820. }
  821. else
  822. {
  823. // from now on, we do not support runtime interface change
  824. CustomWinConfiguration->CanApplyInterfaceImmediately = false;
  825. TCustomScpExplorerForm * ScpExplorer = CreateScpExplorer();
  826. CustomWinConfiguration->AppliedInterface = CustomWinConfiguration->Interface;
  827. try
  828. {
  829. // moved inside try .. __finally, because it can fail as well
  830. TerminalManager->ScpExplorer = ScpExplorer;
  831. if ((ParamCommand != pcNone) || !DownloadFile.IsEmpty())
  832. {
  833. Configuration->Usage->Inc(L"CommandLineOperation");
  834. }
  835. if (ParamCommand == pcUpload)
  836. {
  837. Upload(TerminalManager->ActiveTerminal, CommandParams, UseDefaults);
  838. }
  839. else if (ParamCommand == pcFullSynchronize)
  840. {
  841. FullSynchronize(TerminalManager->ActiveTerminal, ScpExplorer,
  842. CommandParams, UseDefaults);
  843. }
  844. else if (ParamCommand == pcSynchronize)
  845. {
  846. Synchronize(TerminalManager->ActiveTerminal, ScpExplorer,
  847. CommandParams, UseDefaults);
  848. }
  849. else if (ParamCommand == pcEdit)
  850. {
  851. Edit(ScpExplorer, CommandParams);
  852. }
  853. else if (!DownloadFile.IsEmpty())
  854. {
  855. Download(TerminalManager->ActiveTerminal, DownloadFile,
  856. UseDefaults);
  857. }
  858. Application->Run();
  859. }
  860. __finally
  861. {
  862. TerminalManager->ScpExplorer = NULL;
  863. SAFE_DESTROY(ScpExplorer);
  864. }
  865. }
  866. }
  867. catch (Exception &E)
  868. {
  869. ShowExtendedException(&E);
  870. }
  871. }
  872. }
  873. __finally
  874. {
  875. delete DataList;
  876. }
  877. }
  878. while (Retry);
  879. }
  880. }
  881. __finally
  882. {
  883. delete NonVisualDataModule;
  884. NonVisualDataModule = NULL;
  885. delete GlyphsModule;
  886. GlyphsModule = NULL;
  887. TTerminalManager::DestroyInstance();
  888. delete CommandParams;
  889. }
  890. return 0;
  891. }