WinMain.cpp 11 KB

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