WinMain.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <stdio.h>
  5. #include <Interface.h>
  6. #include <Common.h>
  7. #include <Configuration.h>
  8. #include <ScpMain.h>
  9. #include <Terminal.h>
  10. #include <Log.h>
  11. #include <TextsWin.h>
  12. #include "CustomScpExplorer.h"
  13. #include "TerminalManager.h"
  14. #include "NonVisual.h"
  15. #include "ProgParams.h"
  16. #include "Tools.h"
  17. #include "WinConfiguration.h"
  18. #include <NMHttp.hpp>
  19. #include <Psock.hpp>
  20. //---------------------------------------------------------------------------
  21. #pragma package(smart_init)
  22. //---------------------------------------------------------------------------
  23. TSessionData * GetLoginData(AnsiString SessionName)
  24. {
  25. bool ProtocolDefined = false;
  26. TFSProtocol Protocol;
  27. if (SessionName.SubString(1, 6).LowerCase() == "scp://")
  28. {
  29. Protocol = fsSCPonly;
  30. SessionName.Delete(1, 6);
  31. ProtocolDefined = true;
  32. }
  33. else if (SessionName.SubString(1, 7).LowerCase() == "sftp://")
  34. {
  35. Protocol = fsSFTPonly;
  36. SessionName.Delete(1, 7);
  37. ProtocolDefined = true;
  38. }
  39. bool DefaultsOnly = true;
  40. TSessionData *Data = new TSessionData("");
  41. if (!SessionName.IsEmpty())
  42. {
  43. TSessionData * AData;
  44. // lookup stored session session even if protocol was defined
  45. // (this allows setting for example default username for host
  46. // by creating stored session named by host)
  47. AnsiString ASessionName(SessionName);
  48. if (ASessionName[ASessionName.Length()] == '/')
  49. {
  50. ASessionName.SetLength(ASessionName.Length() - 1);
  51. }
  52. AData = (TSessionData *)StoredSessions->FindByName(ASessionName, False);
  53. if (!AData)
  54. {
  55. Data->Assign(StoredSessions->DefaultSettings);
  56. if (Data->ParseUrl(SessionName, 0))
  57. {
  58. Data->Name = "";
  59. DefaultsOnly = false;
  60. }
  61. else
  62. {
  63. SimpleErrorDialog(FMTLOAD(SESSION_NOT_EXISTS_ERROR, (SessionName)));
  64. }
  65. }
  66. else
  67. {
  68. DefaultsOnly = false;
  69. Data->Assign(AData);
  70. if (StoredSessions->IsHidden(AData))
  71. {
  72. AData->Remove();
  73. StoredSessions->Remove(AData);
  74. StoredSessions->Save();
  75. }
  76. }
  77. }
  78. else
  79. {
  80. Data->Assign(StoredSessions->DefaultSettings);
  81. }
  82. if (ProtocolDefined)
  83. {
  84. Data->FSProtocol = Protocol;
  85. }
  86. if (!Data->CanLogin || DefaultsOnly)
  87. {
  88. if (!DoLoginDialog(StoredSessions, Data, loStartup) || !Data->CanLogin)
  89. {
  90. delete Data;
  91. Data = NULL;
  92. }
  93. }
  94. return Data;
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall Upload(TTerminal * Terminal, TProgramParams * Params,
  98. int ListFrom, int ListTo)
  99. {
  100. AnsiString TargetDirectory;
  101. TGUICopyParamType CopyParam = GUIConfiguration->CopyParam;
  102. TStrings * FileList = NULL;
  103. try
  104. {
  105. FileList = new TStringList();
  106. for (int Index = ListFrom; Index <= ListTo; Index++)
  107. {
  108. FileList->Add(Params->Param[Index]);
  109. }
  110. TargetDirectory = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  111. int Options = coDisableQueue |
  112. (!Terminal->IsCapable[fcTextMode] ? coDisableTransferMode : 0);
  113. if (DoCopyDialog(true, false, FileList, TargetDirectory, &CopyParam, Options))
  114. {
  115. int Params = 0;
  116. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
  117. }
  118. }
  119. __finally
  120. {
  121. delete FileList;
  122. }
  123. }
  124. //---------------------------------------------------------------------------
  125. int __fastcall CalculateCompoundVersion(int MajorVer,
  126. int MinorVer, int Release, int Build)
  127. {
  128. int CompoundVer = Build + 1000 * (Release + 100 * (MinorVer +
  129. 100 * MajorVer));
  130. return CompoundVer;
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall RegisterAsUrlHandler()
  134. {
  135. try
  136. {
  137. bool Success;
  138. bool User = true;
  139. TRegistry * Registry = new TRegistry();
  140. try
  141. {
  142. do
  143. {
  144. Success = true;
  145. User = !User;
  146. try
  147. {
  148. assert(Configuration != NULL);
  149. AnsiString FileName = Application->ExeName;
  150. AnsiString BaseKey;
  151. Registry->Access = KEY_WRITE;
  152. if (User)
  153. {
  154. Registry->RootKey = HKEY_CURRENT_USER;
  155. BaseKey = "Software\\Classes\\";
  156. }
  157. else
  158. {
  159. Registry->RootKey = HKEY_CLASSES_ROOT;
  160. BaseKey = "";
  161. }
  162. AnsiString Protocol;
  163. for (int Index = 0; Index <= 1; Index++)
  164. {
  165. Protocol = (Index == 0) ? "SCP" : "SFTP";
  166. if (Registry->OpenKey(BaseKey + Protocol, true))
  167. {
  168. Registry->WriteString("", FMTLOAD(PROTOCOL_URL_DESC, (Protocol)));
  169. Registry->WriteString("URL Protocol", "");
  170. Registry->WriteInteger("EditFlags", 0x02);
  171. Registry->WriteInteger("BrowserFlags", 0x08);
  172. if (Registry->OpenKey("DefaultIcon", true))
  173. {
  174. Registry->WriteString("", FORMAT("\"%s\",0", (FileName)));
  175. Registry->CloseKey();
  176. }
  177. else
  178. {
  179. Abort();
  180. }
  181. }
  182. else
  183. {
  184. Abort();
  185. }
  186. if (Registry->OpenKey(BaseKey + Protocol, false) &&
  187. Registry->OpenKey("shell", true) &&
  188. Registry->OpenKey("open", true) &&
  189. Registry->OpenKey("command", true))
  190. {
  191. Registry->WriteString("", FORMAT("\"%s\" %%1", (FileName)));
  192. Registry->CloseKey();
  193. }
  194. else
  195. {
  196. Abort();
  197. }
  198. }
  199. }
  200. catch(...)
  201. {
  202. Success = false;
  203. }
  204. }
  205. while (!Success && !User);
  206. }
  207. __finally
  208. {
  209. delete Registry;
  210. }
  211. }
  212. catch(Exception & E)
  213. {
  214. throw ExtException(&E, LoadStr(REGISTER_URL_ERROR));
  215. }
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall CheckForUpdates()
  219. {
  220. bool Found = false;
  221. TCustomForm * ActiveForm = Screen->ActiveCustomForm;
  222. Busy(true);
  223. try
  224. {
  225. if (ActiveForm)
  226. {
  227. assert(ActiveForm->Enabled);
  228. ActiveForm->Enabled = false;
  229. }
  230. try
  231. {
  232. AnsiString Response;
  233. TNMHTTP * CheckForUpdatesHTTP = new TNMHTTP(Application);
  234. try
  235. {
  236. CheckForUpdatesHTTP->Get(LoadStr(UPDATES_URL));
  237. Response = CheckForUpdatesHTTP->Body;
  238. }
  239. __finally
  240. {
  241. delete CheckForUpdatesHTTP;
  242. }
  243. while (!Response.IsEmpty() && !Found)
  244. {
  245. AnsiString Line = ::CutToChar(Response, '\n', false);
  246. AnsiString Name = ::CutToChar(Line, '=', false);
  247. if (AnsiSameText(Name, "Version"))
  248. {
  249. Found = true;
  250. int MajorVer = StrToInt(::CutToChar(Line, '.', false));
  251. int MinorVer = StrToInt(::CutToChar(Line, '.', false));
  252. int Release = StrToInt(::CutToChar(Line, '.', false));
  253. int Build = StrToInt(::CutToChar(Line, '.', false));
  254. int CompoundVer = CalculateCompoundVersion(MajorVer, MinorVer, Release, Build);
  255. AnsiString VersionStr =
  256. FORMAT("%d.%d", (MajorVer, MinorVer)) + (Release ? "."+IntToStr(Release) : AnsiString());
  257. TVSFixedFileInfo * FileInfo = Configuration->FixedApplicationInfo;
  258. int CurrentCompoundVer = CalculateCompoundVersion(
  259. HIWORD(FileInfo->dwFileVersionMS), LOWORD(FileInfo->dwFileVersionMS),
  260. HIWORD(FileInfo->dwFileVersionLS), LOWORD(FileInfo->dwFileVersionLS));
  261. if (CurrentCompoundVer < CompoundVer)
  262. {
  263. if (MessageDialog(FMTLOAD(NEW_VERSION, (VersionStr)), qtInformation,
  264. qaOK | qaCancel, 0) == qaOK)
  265. {
  266. NonVisualDataModule->OpenBrowser(LoadStr(DOWNLOAD_URL));
  267. }
  268. }
  269. else
  270. {
  271. MessageDialog(LoadStr(NO_NEW_VERSION), qtInformation, qaOK, 0);
  272. }
  273. }
  274. }
  275. }
  276. catch(Exception & E)
  277. {
  278. throw ExtException(&E, LoadStr(CHECK_FOR_UPDATES_ERROR));
  279. }
  280. }
  281. __finally
  282. {
  283. if (ActiveForm)
  284. {
  285. ActiveForm->Enabled = true;
  286. }
  287. Busy(false);
  288. }
  289. if (!Found)
  290. {
  291. throw Exception(LoadStr(CHECK_FOR_UPDATES_ERROR));
  292. }
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall Execute(TProgramParams * Params)
  296. {
  297. assert(StoredSessions);
  298. assert(Params);
  299. TTerminalManager * TerminalManager = NULL;
  300. NonVisualDataModule = NULL;
  301. try
  302. {
  303. TerminalManager = TTerminalManager::Instance();
  304. NonVisualDataModule = new TNonVisualDataModule(Application);
  305. LogForm = NULL;
  306. Application->HintHidePause = 1000;
  307. if (Params->FindSwitch("UninstallCleanup"))
  308. {
  309. if (MessageDialog(LoadStr(UNINSTALL_CLEANUP), qtConfirmation,
  310. qaOK | qaCancel, 0) == qaOK)
  311. {
  312. DoCleanupDialog(StoredSessions, Configuration);
  313. }
  314. }
  315. else if (Params->FindSwitch("RegisterAsUrlHandler"))
  316. {
  317. RegisterAsUrlHandler();
  318. }
  319. else if (Params->FindSwitch("Update"))
  320. {
  321. CheckForUpdates();
  322. }
  323. else
  324. {
  325. TSessionData * Data;
  326. int UploadListStart = 0;
  327. AnsiString AutoStartSession;
  328. if (Params->ParamCount)
  329. {
  330. AnsiString DummyValue;
  331. if (Params->FindSwitch("Upload", DummyValue, UploadListStart))
  332. {
  333. UploadListStart++;
  334. if (UploadListStart >= 2)
  335. {
  336. AutoStartSession = Params->Param[1];
  337. }
  338. }
  339. else
  340. {
  341. AutoStartSession = Params->Param[1];
  342. }
  343. }
  344. else if (WinConfiguration->EmbeddedSessions && StoredSessions->Count)
  345. {
  346. AutoStartSession = StoredSessions->Sessions[0]->Name;
  347. }
  348. else
  349. {
  350. AutoStartSession = WinConfiguration->AutoStartSession;
  351. }
  352. Data = GetLoginData(AutoStartSession);
  353. if (Data)
  354. {
  355. try
  356. {
  357. assert(!TerminalManager->ActiveTerminal);
  358. TerminalManager->NewTerminal(Data);
  359. }
  360. __finally
  361. {
  362. delete Data;
  363. }
  364. try
  365. {
  366. if (TerminalManager->ConnectActiveTerminal())
  367. {
  368. TCustomScpExplorerForm * ScpExplorer = CreateScpExplorer();
  369. try
  370. {
  371. // moved inside try .. __finally, because it can fail as well
  372. TerminalManager->ScpExplorer = ScpExplorer;
  373. if (UploadListStart > 0)
  374. {
  375. if (UploadListStart <= Params->ParamCount)
  376. {
  377. Upload(TerminalManager->ActiveTerminal, Params,
  378. UploadListStart, Params->ParamCount);
  379. }
  380. else
  381. {
  382. throw Exception(NO_UPLOAD_LIST_ERROR);
  383. }
  384. }
  385. Application->Run();
  386. }
  387. __finally
  388. {
  389. TerminalManager->ScpExplorer = NULL;
  390. SAFE_DESTROY(ScpExplorer);
  391. }
  392. }
  393. }
  394. catch (Exception &E)
  395. {
  396. ShowExtendedException(&E);
  397. }
  398. }
  399. }
  400. }
  401. __finally
  402. {
  403. delete NonVisualDataModule;
  404. NonVisualDataModule = NULL;
  405. TTerminalManager::DestroyInstance();
  406. }
  407. }