GenerateUrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "VCLCommon.h"
  5. #include "GenerateUrl.h"
  6. #include "CoreMain.h"
  7. #include "WinConfiguration.h"
  8. #include <StrUtils.hpp>
  9. #include <Tools.h>
  10. #include <PuttyTools.h>
  11. #include <TextsWin.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #ifndef NO_RESOURCES
  15. #pragma resource "*.dfm"
  16. #endif
  17. //---------------------------------------------------------------------------
  18. void __fastcall DoGenerateUrlDialog(TSessionData * Data, TStrings * Paths)
  19. {
  20. std::unique_ptr<TGenerateUrlDialog> Dialog(new TGenerateUrlDialog(GetFormOwner(), Data, Paths));
  21. Dialog->Execute();
  22. }
  23. //---------------------------------------------------------------------------
  24. __fastcall TGenerateUrlDialog::TGenerateUrlDialog(
  25. TComponent * Owner, TSessionData * Data, TStrings * Paths)
  26. : TForm(Owner)
  27. {
  28. UseSystemSettings(this);
  29. FData = Data;
  30. FPaths = Paths;
  31. FChanging = false;
  32. ReadOnlyControl(ResultMemo);
  33. }
  34. //---------------------------------------------------------------------------
  35. bool __fastcall TGenerateUrlDialog::IsFileUrl()
  36. {
  37. return (FPaths != NULL);
  38. }
  39. //---------------------------------------------------------------------------
  40. UnicodeString __fastcall TGenerateUrlDialog::GenerateUrl(UnicodeString Path)
  41. {
  42. UnicodeString Url =
  43. FData->GenerateSessionUrl(
  44. FLAGMASK(WinSCPSpecificCheck->Checked, sufSpecific) |
  45. FLAGMASK(UserNameCheck->Enabled && UserNameCheck->Checked, sufUserName) |
  46. FLAGMASK(PasswordCheck->Enabled && PasswordCheck->Checked, sufPassword) |
  47. FLAGMASK(HostKeyCheck->Enabled && HostKeyCheck->Checked, sufHostKey));
  48. if ((RemoteDirectoryCheck->Enabled && RemoteDirectoryCheck->Checked) ||
  49. IsFileUrl())
  50. {
  51. if (StartsStr(L"/", Path));
  52. {
  53. Path.Delete(1, 1);
  54. }
  55. Url += EncodeUrlPath(Path);
  56. }
  57. if (SaveExtensionCheck->Enabled && SaveExtensionCheck->Checked)
  58. {
  59. Url += UnicodeString(UrlParamSeparator) + UrlSaveParamName;
  60. }
  61. return Url;
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TGenerateUrlDialog::UpdateControls()
  65. {
  66. if (!FChanging)
  67. {
  68. UnicodeString ExeName = Application->ExeName;
  69. Caption = LoadStr(IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE);
  70. ScriptSheet->TabVisible = !IsFileUrl();
  71. AssemblySheet->TabVisible = !IsFileUrl();
  72. bool HostKeyUnknown = FData->UsesSsh && FData->HostKey.IsEmpty();
  73. UnicodeString ResultGroupCaption;
  74. if (OptionsPageControl->ActivePage == UrlSheet)
  75. {
  76. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  77. }
  78. else if (OptionsPageControl->ActivePage == ScriptSheet)
  79. {
  80. UnicodeString ScriptDescription;
  81. if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  82. {
  83. ResultGroupCaption = LoadStr(GENERATE_URL_COMMANDLINE_LABEL);
  84. ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (ExeName)))) + L"\n";
  85. }
  86. else
  87. {
  88. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  89. }
  90. if (HostKeyUnknown)
  91. {
  92. ScriptDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  93. }
  94. ScriptDescriptionLabel->Caption = ScriptDescription;
  95. }
  96. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  97. {
  98. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  99. UnicodeString AssemblyDescription;
  100. if (HostKeyUnknown)
  101. {
  102. AssemblyDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  103. }
  104. AssemblyDescriptionLabel->Caption = AssemblyDescription;
  105. }
  106. ResultGroup->Caption = ResultGroupCaption;
  107. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  108. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  109. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  110. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  111. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  112. EnableControl(SaveExtensionCheck, !IsFileUrl());
  113. UnicodeString Result;
  114. bool WordWrap = false;
  115. bool FixedWidth = false;
  116. if (OptionsPageControl->ActivePage == UrlSheet)
  117. {
  118. if (!IsFileUrl())
  119. {
  120. UnicodeString Path = FData->RemoteDirectory;
  121. if (!Path.IsEmpty() && !EndsStr(L"/", Path))
  122. {
  123. Path += L"/";
  124. }
  125. Result = GenerateUrl(Path);
  126. }
  127. else
  128. {
  129. for (int Index = 0; Index < FPaths->Count; Index++)
  130. {
  131. Result += GenerateUrl(FPaths->Strings[Index]) + L"\n";
  132. }
  133. }
  134. WordWrap = true;
  135. }
  136. else if (OptionsPageControl->ActivePage == ScriptSheet)
  137. {
  138. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  139. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  140. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  141. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  142. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  143. {
  144. Result =
  145. FORMAT(
  146. L"open %s\n"
  147. "\n"
  148. "; %s\n"
  149. "; %s\n"
  150. "\n"
  151. "exit\n",
  152. (OpenCommand, CommandPlaceholder1, CommandPlaceholder2));
  153. WordWrap = false;
  154. FixedWidth = true;
  155. }
  156. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  157. {
  158. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  159. Result =
  160. FORMAT(
  161. L"@echo off\n"
  162. "\n"
  163. "\"%s\" /log=%s.log /ini=nul /command ^\n"
  164. " \"open %s\" ^\n"
  165. " \"%s\" ^\n"
  166. " \"%s\" ^\n"
  167. " \"exit\"\n"
  168. "\n"
  169. "set WINSCP_RESULT=%%ERRORLEVEL%%\n"
  170. "if %%WINSCP_RESULT%% equ 0 (\n"
  171. " echo Success\n"
  172. ") else (\n"
  173. " echo Error\n"
  174. ")\n"
  175. "\n"
  176. "exit /b %%WINSCP_RESULT%%\n",
  177. (ComExeName, BaseExeName, EscapeParam(ReplaceStr(OpenCommand, L"%", L"%%")), CommandPlaceholder1, CommandPlaceholder2));
  178. WordWrap = false;
  179. FixedWidth = true;
  180. }
  181. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  182. {
  183. Result =
  184. FORMAT(
  185. L"/log=%s.log /ini=nul /command \"open %s\" \"%s\" \"%s\" \"exit\"",
  186. (BaseExeName, EscapeParam(OpenCommand), CommandPlaceholder1, CommandPlaceholder2));
  187. WordWrap = true;
  188. FixedWidth = false;
  189. }
  190. }
  191. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  192. {
  193. Result = FData->GenerateAssemblyCode(static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex));
  194. WordWrap = false;
  195. FixedWidth = true;
  196. }
  197. ResultMemo->WordWrap = WordWrap;
  198. ResultMemo->ScrollBars = WordWrap ? ssVertical : ssBoth;
  199. ResultMemo->Lines->Text = Result;
  200. if (FixedWidth)
  201. {
  202. ResultMemo->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  203. }
  204. else
  205. {
  206. ResultMemo->ParentFont = true;
  207. }
  208. }
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TGenerateUrlDialog::Execute()
  212. {
  213. int Components = WinConfiguration->GenerateUrlComponents;
  214. if (Components < 0)
  215. {
  216. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  217. }
  218. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  219. {
  220. TAutoFlag ChangingFlag(FChanging);
  221. if (IsFileUrl())
  222. {
  223. OptionsPageControl->ActivePage = UrlSheet;
  224. }
  225. else
  226. {
  227. switch (Target)
  228. {
  229. case guctUrl:
  230. OptionsPageControl->ActivePage = UrlSheet;
  231. break;
  232. case guctScript:
  233. OptionsPageControl->ActivePage = ScriptSheet;
  234. break;
  235. case guctAssembly:
  236. OptionsPageControl->ActivePage = AssemblySheet;
  237. break;
  238. default:
  239. DebugFail();
  240. }
  241. }
  242. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  243. {
  244. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  245. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)))
  246. {
  247. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  248. }
  249. }
  250. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  251. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  252. }
  253. UpdateControls();
  254. ShowModal();
  255. // Do not save the selection for files as the "URL" was selected implicitly
  256. if (!IsFileUrl())
  257. {
  258. if (OptionsPageControl->ActivePage == UrlSheet)
  259. {
  260. Target = guctUrl;
  261. }
  262. else if (OptionsPageControl->ActivePage == ScriptSheet)
  263. {
  264. Target = guctScript;
  265. }
  266. else if (OptionsPageControl->ActivePage == AssemblySheet)
  267. {
  268. Target = guctAssembly;
  269. }
  270. else
  271. {
  272. DebugFail();
  273. }
  274. WinConfiguration->GenerateUrlCodeTarget = Target;
  275. }
  276. if (Target == guctUrl)
  277. {
  278. Components = 0;
  279. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  280. {
  281. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  282. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  283. CheckBox->Checked)
  284. {
  285. Components |= CheckBox->Tag;
  286. }
  287. }
  288. WinConfiguration->GenerateUrlComponents = Components;
  289. }
  290. else if (Target == guctScript)
  291. {
  292. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  293. }
  294. else if (Target == guctAssembly)
  295. {
  296. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  297. }
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  301. {
  302. UpdateControls();
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  306. {
  307. TInstantOperationVisualizer Visualizer;
  308. CopyToClipboard(ResultMemo->Lines);
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  312. {
  313. FormHelp(this);
  314. }
  315. //---------------------------------------------------------------------------
  316. void __fastcall TGenerateUrlDialog::WMNCCreate(TWMNCCreate & Message)
  317. {
  318. // bypass TForm::WMNCCreate to prevent disabling "resize"
  319. // (which is done for bsDialog, see comments in CreateParams)
  320. DefaultHandler(&Message);
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TGenerateUrlDialog::Dispatch(void * AMessage)
  324. {
  325. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  326. if (Message.Msg == WM_NCCREATE)
  327. {
  328. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  329. }
  330. else
  331. {
  332. TForm::Dispatch(AMessage);
  333. }
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TGenerateUrlDialog::CreateParams(TCreateParams & Params)
  337. {
  338. TForm::CreateParams(Params);
  339. // Allow resizing of the window, even if this is bsDialog.
  340. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  341. // reason receive focus, if window is shown atop non-main window
  342. // (like editor)
  343. Params.Style = Params.Style | WS_THICKFRAME;
  344. }
  345. //---------------------------------------------------------------------------