1
0

GenerateUrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. Caption = LoadStr(IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE);
  69. ScriptSheet->TabVisible = !IsFileUrl();
  70. AssemblySheet->TabVisible = !IsFileUrl();
  71. UnicodeString ResultGroupCaption;
  72. if (OptionsPageControl->ActivePage == UrlSheet)
  73. {
  74. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  75. }
  76. else if (OptionsPageControl->ActivePage == ScriptSheet)
  77. {
  78. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  79. }
  80. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  81. {
  82. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  83. }
  84. ResultGroup->Caption = ResultGroupCaption;
  85. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  86. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  87. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  88. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  89. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  90. EnableControl(SaveExtensionCheck, !IsFileUrl());
  91. UnicodeString Result;
  92. bool WordWrap = false;
  93. bool FixedWidth = false;
  94. if (OptionsPageControl->ActivePage == UrlSheet)
  95. {
  96. if (!IsFileUrl())
  97. {
  98. UnicodeString Path = FData->RemoteDirectory;
  99. if (!Path.IsEmpty() && !EndsStr(L"/", Path))
  100. {
  101. Path += L"/";
  102. }
  103. Result = GenerateUrl(Path);
  104. }
  105. else
  106. {
  107. for (int Index = 0; Index < FPaths->Count; Index++)
  108. {
  109. Result += GenerateUrl(FPaths->Strings[Index]) + L"\n";
  110. }
  111. }
  112. WordWrap = true;
  113. }
  114. else if (OptionsPageControl->ActivePage == ScriptSheet)
  115. {
  116. UnicodeString ExeName = Application->ExeName;
  117. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  118. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  119. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  120. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  121. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  122. {
  123. Result =
  124. FORMAT(
  125. L"open %s\n"
  126. "\n"
  127. "; %s\n"
  128. "; %s\n"
  129. "\n"
  130. "exit\n",
  131. (OpenCommand, CommandPlaceholder1, CommandPlaceholder2));
  132. WordWrap = false;
  133. FixedWidth = true;
  134. }
  135. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  136. {
  137. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  138. Result =
  139. FORMAT(
  140. L"@echo off\n"
  141. "\n"
  142. "\"%s\" /log=%s.log /ini=nul /command ^\n"
  143. " \"open %s\" ^\n"
  144. " \"%s\" ^\n"
  145. " \"%s\" ^\n"
  146. " \"exit\"\n"
  147. "\n"
  148. "set WINSCP_RESULT=%%ERRORLEVEL%%\n"
  149. "if %%WINSCP_RESULT%% equ 0 (\n"
  150. " echo Success\n"
  151. ") else (\n"
  152. " echo Error\n"
  153. ")\n"
  154. "\n"
  155. "exit /b %%WINSCP_RESULT%%\n",
  156. (ComExeName, BaseExeName, EscapeParam(ReplaceStr(OpenCommand, L"%", L"%%")), CommandPlaceholder1, CommandPlaceholder2));
  157. WordWrap = false;
  158. FixedWidth = true;
  159. }
  160. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  161. {
  162. Result =
  163. FORMAT(
  164. L"\"%s\" /console /log=%s.log /ini=nul /command \"open %s\" \"%s\" \"%s\" \"exit\"",
  165. (ExeName, BaseExeName, EscapeParam(OpenCommand), CommandPlaceholder1, CommandPlaceholder2));
  166. WordWrap = true;
  167. FixedWidth = false;
  168. }
  169. }
  170. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  171. {
  172. Result = FData->GenerateAssemblyCode(static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex));
  173. WordWrap = false;
  174. FixedWidth = true;
  175. }
  176. ResultMemo->WordWrap = WordWrap;
  177. ResultMemo->ScrollBars = WordWrap ? ssVertical : ssBoth;
  178. ResultMemo->Lines->Text = Result;
  179. if (FixedWidth)
  180. {
  181. ResultMemo->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  182. }
  183. else
  184. {
  185. ResultMemo->ParentFont = true;
  186. }
  187. }
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall TGenerateUrlDialog::Execute()
  191. {
  192. int Components = WinConfiguration->GenerateUrlComponents;
  193. if (Components < 0)
  194. {
  195. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  196. }
  197. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  198. {
  199. TAutoFlag ChangingFlag(FChanging);
  200. if (IsFileUrl())
  201. {
  202. OptionsPageControl->ActivePage = UrlSheet;
  203. }
  204. else
  205. {
  206. switch (Target)
  207. {
  208. case guctUrl:
  209. OptionsPageControl->ActivePage = UrlSheet;
  210. break;
  211. case guctScript:
  212. OptionsPageControl->ActivePage = ScriptSheet;
  213. break;
  214. case guctAssembly:
  215. OptionsPageControl->ActivePage = AssemblySheet;
  216. break;
  217. default:
  218. DebugFail();
  219. }
  220. }
  221. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  222. {
  223. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  224. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)))
  225. {
  226. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  227. }
  228. }
  229. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  230. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  231. }
  232. UpdateControls();
  233. ShowModal();
  234. // Do not save the selection for files as the "URL" was selected implicitly
  235. if (!IsFileUrl())
  236. {
  237. if (OptionsPageControl->ActivePage == UrlSheet)
  238. {
  239. Target = guctUrl;
  240. }
  241. else if (OptionsPageControl->ActivePage == ScriptSheet)
  242. {
  243. Target = guctScript;
  244. }
  245. else if (OptionsPageControl->ActivePage == AssemblySheet)
  246. {
  247. Target = guctAssembly;
  248. }
  249. else
  250. {
  251. DebugFail();
  252. }
  253. WinConfiguration->GenerateUrlCodeTarget = Target;
  254. }
  255. if (Target == guctUrl)
  256. {
  257. Components = 0;
  258. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  259. {
  260. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  261. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  262. CheckBox->Checked)
  263. {
  264. Components |= CheckBox->Tag;
  265. }
  266. }
  267. WinConfiguration->GenerateUrlComponents = Components;
  268. }
  269. else if (Target == guctScript)
  270. {
  271. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  272. }
  273. else if (Target == guctAssembly)
  274. {
  275. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  276. }
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  280. {
  281. UpdateControls();
  282. }
  283. //---------------------------------------------------------------------------
  284. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  285. {
  286. TInstantOperationVisualizer Visualizer;
  287. CopyToClipboard(ResultMemo->Lines);
  288. }
  289. //---------------------------------------------------------------------------
  290. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  291. {
  292. FormHelp(this);
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall TGenerateUrlDialog::WMNCCreate(TWMNCCreate & Message)
  296. {
  297. // bypass TForm::WMNCCreate to prevent disabling "resize"
  298. // (which is done for bsDialog, see comments in CreateParams)
  299. DefaultHandler(&Message);
  300. }
  301. //---------------------------------------------------------------------------
  302. void __fastcall TGenerateUrlDialog::Dispatch(void * AMessage)
  303. {
  304. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  305. if (Message.Msg == WM_NCCREATE)
  306. {
  307. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  308. }
  309. else
  310. {
  311. TForm::Dispatch(AMessage);
  312. }
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TGenerateUrlDialog::CreateParams(TCreateParams & Params)
  316. {
  317. TForm::CreateParams(Params);
  318. // Allow resizing of the window, even if this is bsDialog.
  319. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  320. // reason receive focus, if window is shown atop non-main window
  321. // (like editor)
  322. Params.Style = Params.Style | WS_THICKFRAME;
  323. }
  324. //---------------------------------------------------------------------------