GenerateUrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. FGroupBoxPadding = ResultGroup->Top - (AssemblyOptionsGroup->Top + AssemblyOptionsGroup->Height);
  34. ScriptOptionsGroup->Top = OptionsGroup->Top;
  35. AssemblyOptionsGroup->Top = OptionsGroup->Top;
  36. int DesiredHeight = ScaleByTextHeight(this, 360);
  37. int HeightChange = Height - DesiredHeight;
  38. // Need to enlarge the results box before it would get out of form
  39. ResultGroup->SetBounds(ResultGroup->Left, ResultGroup->Top - HeightChange, ResultGroup->Width, ResultGroup->Height + HeightChange);
  40. Height = Height - HeightChange;
  41. }
  42. //---------------------------------------------------------------------------
  43. bool __fastcall TGenerateUrlDialog::IsFileUrl()
  44. {
  45. return (FPaths != NULL);
  46. }
  47. //---------------------------------------------------------------------------
  48. UnicodeString __fastcall TGenerateUrlDialog::GenerateUrl(UnicodeString Path)
  49. {
  50. UnicodeString Url =
  51. FData->GenerateSessionUrl(
  52. FLAGMASK(WinSCPSpecificCheck->Checked, sufSpecific) |
  53. FLAGMASK(UserNameCheck->Enabled && UserNameCheck->Checked, sufUserName) |
  54. FLAGMASK(PasswordCheck->Enabled && PasswordCheck->Checked, sufPassword) |
  55. FLAGMASK(HostKeyCheck->Enabled && HostKeyCheck->Checked, sufHostKey));
  56. if ((RemoteDirectoryCheck->Enabled && RemoteDirectoryCheck->Checked) ||
  57. IsFileUrl())
  58. {
  59. if (StartsStr(L"/", Path));
  60. {
  61. Path.Delete(1, 1);
  62. }
  63. Url += EncodeUrlPath(Path);
  64. }
  65. if (SaveExtensionCheck->Enabled && SaveExtensionCheck->Checked)
  66. {
  67. Url += UnicodeString(UrlParamSeparator) + UrlSaveParamName;
  68. }
  69. return Url;
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TGenerateUrlDialog::UpdateControls()
  73. {
  74. if (!FChanging)
  75. {
  76. Caption = LoadStr(IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE);
  77. EnableControl(ScriptButton, !IsFileUrl());
  78. EnableControl(AssemblyButton, !IsFileUrl());
  79. OptionsGroup->Visible = UrlButton->Checked;
  80. ScriptOptionsGroup->Visible = ScriptButton->Checked;
  81. AssemblyOptionsGroup->Visible = AssemblyButton->Checked;
  82. TControl * ResultGroupBelow = NULL;
  83. UnicodeString ResultGroupCaption;
  84. if (UrlButton->Checked)
  85. {
  86. ResultGroupBelow = OptionsGroup;
  87. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  88. }
  89. else if (ScriptButton->Checked)
  90. {
  91. ResultGroupBelow = ScriptOptionsGroup;
  92. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  93. }
  94. else if (ALWAYS_TRUE(AssemblyButton->Checked))
  95. {
  96. ResultGroupBelow = AssemblyOptionsGroup;
  97. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  98. }
  99. ResultGroup->Caption = ResultGroupCaption;
  100. int ResultGroupTop = ResultGroupBelow->Top + ResultGroupBelow->Height + FGroupBoxPadding;
  101. ResultGroup->SetBounds(ResultGroup->Left, ResultGroupTop, ResultGroup->Width, (ResultGroup->Top + ResultGroup->Height) - ResultGroupTop);
  102. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  103. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  104. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  105. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  106. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  107. EnableControl(SaveExtensionCheck, !IsFileUrl());
  108. UnicodeString Result;
  109. bool WordWrap = false;
  110. bool FixedWidth = false;
  111. if (UrlButton->Checked)
  112. {
  113. if (!IsFileUrl())
  114. {
  115. UnicodeString Path = FData->RemoteDirectory;
  116. if (!Path.IsEmpty() && !EndsStr(L"/", Path))
  117. {
  118. Path += L"/";
  119. }
  120. Result = GenerateUrl(Path);
  121. }
  122. else
  123. {
  124. for (int Index = 0; Index < FPaths->Count; Index++)
  125. {
  126. Result += GenerateUrl(FPaths->Strings[Index]) + L"\n";
  127. }
  128. }
  129. WordWrap = true;
  130. }
  131. else if (ScriptButton->Checked)
  132. {
  133. UnicodeString ExeName = Application->ExeName;
  134. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  135. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  136. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  137. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  138. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  139. {
  140. Result =
  141. FORMAT(
  142. L"open %s\n"
  143. "\n"
  144. "; %s\n"
  145. "; %s\n"
  146. "\n"
  147. "exit\n",
  148. (OpenCommand, CommandPlaceholder1, CommandPlaceholder2));
  149. WordWrap = false;
  150. FixedWidth = true;
  151. }
  152. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  153. {
  154. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  155. Result =
  156. FORMAT(
  157. L"@echo off\n"
  158. "\n"
  159. "\"%s\" /log=%s.log /ini=nul /command ^\n"
  160. " \"open %s\" ^\n"
  161. " \"%s\" ^\n"
  162. " \"%s\" ^\n"
  163. " \"exit\"\n"
  164. "\n"
  165. "set WINSCP_RESULT=%%ERRORLEVEL%%\n"
  166. "if %%WINSCP_RESULT%% equ 0 (\n"
  167. " echo Success\n"
  168. ") else (\n"
  169. " echo Error\n"
  170. ")\n"
  171. "\n"
  172. "exit /b %%WINSCP_RESULT%%\n",
  173. (ComExeName, BaseExeName, EscapeParam(ReplaceStr(OpenCommand, L"%", L"%%")), CommandPlaceholder1, CommandPlaceholder2));
  174. WordWrap = false;
  175. FixedWidth = true;
  176. }
  177. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  178. {
  179. Result =
  180. FORMAT(
  181. L"\"%s\" /console /log=%s.log /ini=nul /command \"open %s\" \"%s\" \"%s\" \"exit\"",
  182. (ExeName, BaseExeName, EscapeParam(OpenCommand), CommandPlaceholder1, CommandPlaceholder2));
  183. WordWrap = true;
  184. FixedWidth = false;
  185. }
  186. }
  187. else if (ALWAYS_TRUE(AssemblyButton->Checked))
  188. {
  189. Result = FData->GenerateAssemblyCode(static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex));
  190. WordWrap = false;
  191. FixedWidth = true;
  192. }
  193. ResultMemo->WordWrap = WordWrap;
  194. ResultMemo->ScrollBars = WordWrap ? ssVertical : ssBoth;
  195. ResultMemo->Lines->Text = Result;
  196. if (FixedWidth)
  197. {
  198. ResultMemo->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  199. }
  200. else
  201. {
  202. ResultMemo->ParentFont = true;
  203. }
  204. }
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TGenerateUrlDialog::Execute()
  208. {
  209. int Components = WinConfiguration->GenerateUrlComponents;
  210. if (Components < 0)
  211. {
  212. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  213. }
  214. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  215. {
  216. TAutoFlag ChangingFlag(FChanging);
  217. if (IsFileUrl())
  218. {
  219. UrlButton->Checked = true;
  220. }
  221. else
  222. {
  223. switch (Target)
  224. {
  225. case guctUrl:
  226. UrlButton->Checked = true;
  227. break;
  228. case guctScript:
  229. ScriptButton->Checked = true;
  230. break;
  231. case guctAssembly:
  232. AssemblyButton->Checked = true;
  233. break;
  234. default:
  235. FAIL;
  236. }
  237. }
  238. for (int Index = 0; Index < OptionsGroup->ControlCount; Index++)
  239. {
  240. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(OptionsGroup->Controls[Index]);
  241. if (ALWAYS_TRUE((CheckBox != NULL) && (CheckBox->Tag != 0)))
  242. {
  243. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  244. }
  245. }
  246. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  247. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  248. }
  249. UpdateControls();
  250. ShowModal();
  251. // Do not save the selection for files as the "URL" was selected implicitly
  252. if (!IsFileUrl())
  253. {
  254. if (UrlButton->Checked)
  255. {
  256. Target = guctUrl;
  257. }
  258. else if (ScriptButton->Checked)
  259. {
  260. Target = guctScript;
  261. }
  262. else if (AssemblyButton->Checked)
  263. {
  264. Target = guctAssembly;
  265. }
  266. else
  267. {
  268. FAIL;
  269. }
  270. WinConfiguration->GenerateUrlCodeTarget = Target;
  271. }
  272. if (Target == guctUrl)
  273. {
  274. Components = 0;
  275. for (int Index = 0; Index < OptionsGroup->ControlCount; Index++)
  276. {
  277. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(OptionsGroup->Controls[Index]);
  278. if (ALWAYS_TRUE((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  279. CheckBox->Checked)
  280. {
  281. Components |= CheckBox->Tag;
  282. }
  283. }
  284. WinConfiguration->GenerateUrlComponents = Components;
  285. }
  286. else if (Target == guctScript)
  287. {
  288. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  289. }
  290. else if (Target == guctAssembly)
  291. {
  292. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  293. }
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  297. {
  298. UpdateControls();
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  302. {
  303. TInstantOperationVisualizer Visualizer;
  304. CopyToClipboard(ResultMemo->Lines);
  305. }
  306. //---------------------------------------------------------------------------
  307. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  308. {
  309. FormHelp(this);
  310. }
  311. //---------------------------------------------------------------------------