GenerateUrl.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. #include <ProgParams.h>
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. #ifndef NO_RESOURCES
  16. #pragma resource "*.dfm"
  17. #endif
  18. //---------------------------------------------------------------------------
  19. void __fastcall DoGenerateUrlDialog(TSessionData * Data, TStrings * Paths)
  20. {
  21. std::unique_ptr<TGenerateUrlDialog> Dialog(
  22. new TGenerateUrlDialog(GetFormOwner(), Data, fsList, Paths, false, false, false, 0, UnicodeString(), TCopyParamType()));
  23. Dialog->Execute();
  24. }
  25. //---------------------------------------------------------------------------
  26. void __fastcall DoGenerateTransferCodeDialog(
  27. bool ToRemote, bool Move, int CopyParamAttrs, TSessionData * Data, TFilesSelected FilesSelected, TStrings * FileList, const UnicodeString & Path,
  28. const TCopyParamType & CopyParam)
  29. {
  30. std::unique_ptr<TGenerateUrlDialog> Dialog(
  31. new TGenerateUrlDialog(GetFormOwner(), Data, FilesSelected, FileList, true, ToRemote, Move, CopyParamAttrs, Path, CopyParam));
  32. Dialog->Execute();
  33. }
  34. //---------------------------------------------------------------------------
  35. // Rich edit 4.1 supports "Friendly name hyperlinks"
  36. class TRichEdit41 : public TRichEdit
  37. {
  38. public:
  39. virtual __fastcall TRichEdit41(TComponent * AOwner);
  40. protected:
  41. virtual void __fastcall CreateWnd();
  42. virtual void __fastcall CreateParams(TCreateParams & Params);
  43. virtual void __fastcall DestroyWnd();
  44. void __fastcall Dispatch(void * Message);
  45. private:
  46. HINSTANCE FLibrary;
  47. };
  48. //---------------------------------------------------------------------------
  49. __fastcall TRichEdit41::TRichEdit41(TComponent * AOwner) :
  50. TRichEdit(AOwner),
  51. FLibrary(0)
  52. {
  53. }
  54. //---------------------------------------------------------------------------
  55. void __fastcall TRichEdit41::CreateParams(TCreateParams & Params)
  56. {
  57. UnicodeString RichEditModuleName(L"MSFTEDIT.DLL");
  58. long int OldError;
  59. OldError = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  60. FLibrary = LoadLibrary(RichEditModuleName.c_str());
  61. SetErrorMode(OldError);
  62. TCustomMemo::CreateParams(Params);
  63. // Should not happen as
  64. if (FLibrary != 0)
  65. {
  66. // MSDN says that we should use MSFTEDIT_CLASS to load Rich Edit 4.1:
  67. // https://msdn.microsoft.com/en-us/library/windows/desktop/bb787873.aspx
  68. // But MSFTEDIT_CLASS is defined as "RICHEDIT50W",
  69. // so not sure what version we are loading.
  70. // Seem to work on Windows XP SP3.
  71. CreateSubClass(Params, MSFTEDIT_CLASS);
  72. }
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TRichEdit41::CreateWnd()
  76. {
  77. TRichEdit::CreateWnd();
  78. int Mask = SendMessage(Handle, EM_GETEVENTMASK, 0, 0);
  79. SendMessage(Handle, EM_SETEVENTMASK, 0, Mask | ENM_LINK);
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TRichEdit41::Dispatch(void * AMessage)
  83. {
  84. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  85. if (Message.Msg == CN_NOTIFY)
  86. {
  87. TWMNotify & WMNotify = *reinterpret_cast<TWMNotify *>(AMessage);
  88. if (WMNotify.NMHdr->code == EN_LINK)
  89. {
  90. TENLink & ENLink = *reinterpret_cast<TENLink *>(Message.LParam);
  91. if (ENLink.msg == WM_LBUTTONDOWN)
  92. {
  93. UnicodeString AText = Text;
  94. // The cpMin and cpMax refer to indexes in a script with a single-byte EOL,
  95. // while the Text (GetWindowText) uses two-byte EOL
  96. AText = ReplaceStr(AText, L"\r\n", L"\n");
  97. if (DebugAlwaysTrue(ENLink.chrg.cpMax < AText.Length()))
  98. {
  99. UnicodeString Url = AText.SubString(ENLink.chrg.cpMin + 1, ENLink.chrg.cpMax - ENLink.chrg.cpMin);
  100. ShowHelp(Url);
  101. }
  102. }
  103. }
  104. TRichEdit::Dispatch(AMessage);
  105. }
  106. else
  107. {
  108. TRichEdit::Dispatch(AMessage);
  109. }
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TRichEdit41::DestroyWnd()
  113. {
  114. TRichEdit::DestroyWnd();
  115. if (FLibrary != 0)
  116. {
  117. FreeLibrary(FLibrary);
  118. }
  119. }
  120. //---------------------------------------------------------------------------
  121. //---------------------------------------------------------------------------
  122. __fastcall TGenerateUrlDialog::TGenerateUrlDialog(
  123. TComponent * Owner, TSessionData * Data, TFilesSelected FilesSelected, TStrings * Paths,
  124. bool Transfer, bool ToRemote, bool Move, int CopyParamAttrs, const UnicodeString & Path, const TCopyParamType & CopyParam)
  125. : TForm(Owner)
  126. {
  127. UseSystemSettings(this);
  128. FData = Data;
  129. if (Paths != NULL)
  130. {
  131. FPaths.reset(new TStringList());
  132. FPaths->AddStrings(Paths);
  133. }
  134. FTransfer = Transfer;
  135. FToRemote = ToRemote;
  136. FMove = Move;
  137. FCopyParamAttrs = CopyParamAttrs;
  138. FCopyParam = CopyParam;
  139. if (FTransfer)
  140. {
  141. DebugAssert(FPaths.get() != NULL);
  142. if (FToRemote)
  143. {
  144. UnicodeString FirstPath = Paths->Strings[0];
  145. FSourcePath = FToRemote ? ExcludeTrailingBackslash(ExtractFilePath(FirstPath)) : UnixExtractFilePath(FirstPath);
  146. for (int Index = 0; Index < FPaths->Count; Index++)
  147. {
  148. FPaths->Strings[Index] = ExtractFileName(FPaths->Strings[Index]);
  149. }
  150. }
  151. else
  152. {
  153. FSourcePath = Data->RemoteDirectory;
  154. // should be noop as we get only file names for remote files
  155. for (int Index = 0; Index < FPaths->Count; Index++)
  156. {
  157. FPaths->Strings[Index] = UnixExtractFileName(FPaths->Strings[Index]);
  158. }
  159. }
  160. }
  161. FPath = Path;
  162. FFilesSelected = FilesSelected;
  163. FChanging = false;
  164. FResultMemo41 = new TRichEdit41(this);
  165. FResultMemo41->Parent = ResultMemo->Parent;
  166. FResultMemo41->SetBounds(ResultMemo->Left, ResultMemo->Top, ResultMemo->Width, ResultMemo->Height);
  167. FResultMemo41->Anchors = ResultMemo->Anchors;
  168. FResultMemo41->BevelInner = ResultMemo->BevelInner;
  169. FResultMemo41->BevelOuter = ResultMemo->BevelOuter;
  170. FResultMemo41->BorderStyle = ResultMemo->BorderStyle;
  171. FResultMemo41->PopupMenu = ResultMemo->PopupMenu;
  172. FResultMemo41->TabOrder = ResultMemo->TabOrder;
  173. FResultMemo41->PlainText = false;
  174. ResultMemo->Visible = false;
  175. ReadOnlyControl(FResultMemo41);
  176. }
  177. //---------------------------------------------------------------------------
  178. bool __fastcall TGenerateUrlDialog::IsFileUrl()
  179. {
  180. return (FPaths.get() != NULL) && !FTransfer;
  181. }
  182. //---------------------------------------------------------------------------
  183. UnicodeString __fastcall TGenerateUrlDialog::GenerateUrl(UnicodeString Path)
  184. {
  185. UnicodeString Url =
  186. FData->GenerateSessionUrl(
  187. FLAGMASK(WinSCPSpecificCheck->Checked, sufSpecific) |
  188. FLAGMASK(UserNameCheck->Enabled && UserNameCheck->Checked, sufUserName) |
  189. FLAGMASK(PasswordCheck->Enabled && PasswordCheck->Checked, sufPassword) |
  190. FLAGMASK(HostKeyCheck->Enabled && HostKeyCheck->Checked, sufHostKey));
  191. if ((RemoteDirectoryCheck->Enabled && RemoteDirectoryCheck->Checked) ||
  192. IsFileUrl())
  193. {
  194. if (StartsStr(L"/", Path));
  195. {
  196. Path.Delete(1, 1);
  197. }
  198. Url += EncodeUrlPath(Path);
  199. }
  200. if (SaveExtensionCheck->Enabled && SaveExtensionCheck->Checked)
  201. {
  202. Url += UnicodeString(UrlParamSeparator) + UrlSaveParamName;
  203. }
  204. return Url;
  205. }
  206. //---------------------------------------------------------------------------
  207. static UnicodeString __fastcall RtfColorEntry(int Color)
  208. {
  209. return FORMAT(L"\\red%d\\green%d\\blue%d;", ((Color & 0xFF0000) >> 16, (Color & 0x00FF00) >> 8, (Color & 0x0000FF) >> 0));
  210. }
  211. //---------------------------------------------------------------------
  212. static UnicodeString __fastcall RtfScriptComment(const UnicodeString & Text)
  213. {
  214. return RtfColorItalicText(7, Text);
  215. }
  216. //---------------------------------------------------------------------
  217. static UnicodeString __fastcall RtfScriptPlaceholder(const UnicodeString & Text)
  218. {
  219. return RtfColorText(7, Text);
  220. }
  221. //---------------------------------------------------------------------
  222. static UnicodeString __fastcall RtfScriptCommand(const UnicodeString & Command)
  223. {
  224. return RtfLink(ScriptCommandLink(Command), RtfKeyword(Command));
  225. }
  226. //---------------------------------------------------------------------
  227. UnicodeString __fastcall RtfCommandlineSwitch(const UnicodeString & Switch, const UnicodeString & Anchor)
  228. {
  229. return RtfLink(L"commandline#" + Anchor, RtfParameter(TProgramParams::FormatSwitch(Switch.LowerCase())));
  230. }
  231. //---------------------------------------------------------------------------
  232. static UnicodeString __fastcall QuoteStringParam(UnicodeString S)
  233. {
  234. return AddQuotes(RtfEscapeParam(S));
  235. }
  236. //---------------------------------------------------------------------------
  237. // Keep in sync with .NET Session.EscapeFileMask
  238. static UnicodeString __fastcall EscapeFileMask(UnicodeString S)
  239. {
  240. return ReplaceStr(ReplaceStr(ReplaceStr(S, L"[", L"[[]"), L"*", L"[*]"), L"?", L"[?]");
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TGenerateUrlDialog::UpdateControls()
  244. {
  245. if (!FChanging)
  246. {
  247. UnicodeString ExeName = Application->ExeName;
  248. int CaptionId;
  249. if (FTransfer)
  250. {
  251. CaptionId = GENERATE_URL_TRANSFER_TITLE;
  252. }
  253. else
  254. {
  255. CaptionId = IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE;
  256. }
  257. Caption = LoadStr(CaptionId);
  258. UrlSheet->TabVisible = !FTransfer;
  259. ScriptSheet->TabVisible = !IsFileUrl();
  260. AssemblySheet->TabVisible = !IsFileUrl();
  261. bool HostKeyUnknown = FData->UsesSsh && FData->HostKey.IsEmpty();
  262. UnicodeString ResultGroupCaption;
  263. if (OptionsPageControl->ActivePage == UrlSheet)
  264. {
  265. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  266. }
  267. else if (OptionsPageControl->ActivePage == ScriptSheet)
  268. {
  269. if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  270. {
  271. ResultGroupCaption = LoadStr(GENERATE_URL_COMMANDLINE_LABEL);
  272. }
  273. else
  274. {
  275. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  276. }
  277. }
  278. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  279. {
  280. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  281. UnicodeString AssemblyDescription;
  282. if (HostKeyUnknown)
  283. {
  284. AssemblyDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  285. }
  286. AssemblyDescriptionLabel->Caption = AssemblyDescription;
  287. }
  288. ResultGroup->Caption = ResultGroupCaption;
  289. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  290. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  291. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  292. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  293. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  294. EnableControl(SaveExtensionCheck, !IsFileUrl());
  295. UnicodeString Result;
  296. bool WordWrap = false;
  297. bool FixedWidth = false;
  298. if (OptionsPageControl->ActivePage == UrlSheet)
  299. {
  300. if (!IsFileUrl())
  301. {
  302. UnicodeString Path = FData->RemoteDirectory;
  303. if (!Path.IsEmpty() && !EndsStr(L"/", Path))
  304. {
  305. Path += L"/";
  306. }
  307. Result = RtfText(GenerateUrl(Path));
  308. }
  309. else
  310. {
  311. for (int Index = 0; Index < FPaths->Count; Index++)
  312. {
  313. UnicodeString Url = GenerateUrl(FPaths->Strings[Index]);
  314. Result += RtfText(Url) + RtfPara;
  315. }
  316. }
  317. WordWrap = true;
  318. }
  319. else if (OptionsPageControl->ActivePage == ScriptSheet)
  320. {
  321. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  322. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  323. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  324. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  325. UnicodeString LogPath = LoadStr(GENERATE_URL_WRITABLE_PATH_TO_LOG) + RtfText(BaseExeName + L".log");
  326. UnicodeString LogParameter =
  327. RtfCommandlineSwitch(LOG_SWITCH, L"logging") + RtfText(L"=") +
  328. RtfScriptPlaceholder(L"\"" + LogPath + L"\"");
  329. UnicodeString IniParameter =
  330. RtfCommandlineSwitch(INI_SWITCH, L"configuration") + RtfText(UnicodeString(L"=") + INI_NUL);
  331. UnicodeString CommandParameter = RtfCommandlineSwitch(COMMAND_SWITCH, L"scripting");
  332. typedef std::vector<UnicodeString> TCommands;
  333. TCommands Commands;
  334. Commands.push_back(RtfScriptCommand(L"open") + L" " + OpenCommand);
  335. Commands.push_back(UnicodeString());
  336. bool NoArgs = false;
  337. if (FTransfer)
  338. {
  339. UnicodeString TransferCommand;
  340. if (FToRemote)
  341. {
  342. Commands.push_back(RtfScriptCommand(L"lcd") + L" " + RtfText(QuoteStringParam(FSourcePath)));
  343. Commands.push_back(RtfScriptCommand(L"cd") + L" " + RtfText(QuoteStringParam(UnixExcludeTrailingBackslash(FPath))));
  344. TransferCommand = L"put";
  345. }
  346. else
  347. {
  348. Commands.push_back(RtfScriptCommand(L"cd") + L" " + RtfText(QuoteStringParam(FSourcePath)));
  349. Commands.push_back(RtfScriptCommand(L"lcd") + L" " + RtfText(QuoteStringParam(ExcludeTrailingBackslash(FPath))));
  350. TransferCommand = L"get";
  351. }
  352. Commands.push_back(UnicodeString());
  353. UnicodeString TransferCommandLink = ScriptCommandLink(TransferCommand);
  354. UnicodeString TransferCommandArgs;
  355. if (FMove)
  356. {
  357. TransferCommandArgs += RtfSwitch(DELETE_SWITCH, TransferCommandLink);
  358. }
  359. TransferCommandArgs += FCopyParam.GenerateTransferCommandArgs(FCopyParamAttrs, TransferCommandLink, NoArgs);
  360. TransferCommand = RtfScriptCommand(TransferCommand) + TransferCommandArgs;
  361. if (FFilesSelected == fsList)
  362. {
  363. for (int Index = 0; Index < FPaths->Count; Index++)
  364. {
  365. Commands.push_back(TransferCommand + L" " + RtfText(QuoteStringParam(EscapeFileMask(FPaths->Strings[Index]))));
  366. }
  367. }
  368. else
  369. {
  370. Commands.push_back(TransferCommand + L" " + RtfText(QuoteStringParam(FSourcePath + L"*")));
  371. }
  372. }
  373. else
  374. {
  375. Commands.push_back(L"# " + CommandPlaceholder1);
  376. Commands.push_back(L"# " + CommandPlaceholder2);
  377. }
  378. Commands.push_back(UnicodeString());
  379. Commands.push_back(RtfScriptCommand(L"exit"));
  380. UnicodeString ScriptDescription;
  381. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  382. {
  383. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  384. {
  385. UnicodeString Command = *I;
  386. if (!Command.IsEmpty())
  387. {
  388. if (Command[1] == L'#')
  389. {
  390. Result += RtfScriptComment(Command);
  391. }
  392. else
  393. {
  394. Result += Command;
  395. }
  396. }
  397. Result += RtfPara;
  398. }
  399. UnicodeString ScriptCommandLine =
  400. FORMAT("\"%s\" /%s=\"%s\" /%s=%s /%s=\"%s\"",
  401. (ExeName, LowerCase(LOG_SWITCH), LogPath, LowerCase(INI_SWITCH), INI_NUL, LowerCase(SCRIPT_SWITCH), LoadStr(GENERATE_URL_PATH_TO_SCRIPT)));
  402. Result +=
  403. RtfPara +
  404. RtfScriptComment(L"# " + LoadStr(GENERATE_URL_SCRIPT_DESC)) + RtfPara +
  405. RtfScriptComment(L"# " + ScriptCommandLine) + RtfPara;
  406. WordWrap = false;
  407. FixedWidth = true;
  408. }
  409. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  410. {
  411. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  412. Result =
  413. RtfScriptPlaceholder(L"@echo off") + RtfPara +
  414. RtfPara +
  415. RtfText(L"\"" + ComExeName + "\" ^") + RtfPara +
  416. RtfText(L" ") + LogParameter + L" " + IniParameter + RtfText(L" ^") + RtfPara +
  417. RtfText(L" ") + CommandParameter;
  418. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  419. {
  420. UnicodeString Command = *I;
  421. if (!Command.IsEmpty())
  422. {
  423. Result +=
  424. RtfText(L" ^") + RtfPara +
  425. RtfText(L" \"");
  426. if (Command[1] == L'#')
  427. {
  428. Command.Delete(1, 1);
  429. Result += RtfScriptPlaceholder(Command.TrimLeft());
  430. }
  431. else
  432. {
  433. Result += RtfEscapeParam(ReplaceStr(Command, L"%", L"%%"));
  434. }
  435. Result += L"\"";
  436. }
  437. }
  438. Result +=
  439. RtfPara +
  440. RtfPara +
  441. RtfKeyword(L"set") + RtfText(L" WINSCP_RESULT=%ERRORLEVEL%") + RtfPara +
  442. RtfKeyword(L"if") + RtfText(L" %WINSCP_RESULT% ") + RtfKeyword(L"equ") + RtfText(L" 0 (") + RtfPara +
  443. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Success") + RtfPara +
  444. RtfText(L") ") + RtfKeyword(L"else") + RtfText(L" (") + RtfPara +
  445. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Error") + RtfPara +
  446. RtfText(L")") + RtfPara +
  447. RtfPara +
  448. RtfKeyword(L"exit") + RtfText(L" /b %WINSCP_RESULT%") + RtfPara;
  449. WordWrap = false;
  450. FixedWidth = true;
  451. }
  452. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  453. {
  454. Result =
  455. LogParameter + L" " +
  456. IniParameter + L" " +
  457. CommandParameter;
  458. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  459. {
  460. UnicodeString Command = *I;
  461. if (!Command.IsEmpty())
  462. {
  463. Result += RtfText(L" \"");
  464. if (Command[1] == L'#')
  465. {
  466. Command.Delete(1, 1);
  467. Result += RtfScriptPlaceholder(Command.TrimLeft());
  468. }
  469. else
  470. {
  471. Result += RtfEscapeParam(Command);
  472. }
  473. Result += L"\"";
  474. }
  475. }
  476. WordWrap = true;
  477. FixedWidth = false;
  478. ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (ExeName)))) + L"\n";
  479. }
  480. if (HostKeyUnknown)
  481. {
  482. ScriptDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  483. }
  484. if (NoArgs)
  485. {
  486. ScriptDescription += LoadStr(GENERATE_URL_COPY_PARAM_SCRIPT_REMAINING) + L"\n";
  487. }
  488. ScriptDescriptionLabel->Caption = ScriptDescription;
  489. }
  490. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  491. {
  492. Result = FData->GenerateAssemblyCode(static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex));
  493. WordWrap = false;
  494. FixedWidth = true;
  495. }
  496. if (FixedWidth)
  497. {
  498. FResultMemo41->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  499. }
  500. else
  501. {
  502. FResultMemo41->ParentFont = true;
  503. }
  504. Result =
  505. L"{\\rtf1\n"
  506. "{\\colortbl ;" +
  507. // The same RGB as on wiki
  508. RtfColorEntry(0x010101) + // near-black fake color to be used with no-style link to ovreride the default blue underline
  509. RtfColorEntry(0x008000) + // code comment (green)
  510. RtfColorEntry(0x008080) + // class (teal)
  511. RtfColorEntry(0x800000) + // string (maroon)
  512. RtfColorEntry(0x0000FF) + // keyword (blue)
  513. RtfColorEntry(0x993333) + // command-line argument (reddish)
  514. RtfColorEntry(0x808080) + // script command (gray)
  515. L"}\n"
  516. "{\\fonttbl{\\f0\\fnil\\fcharset0 " + FResultMemo41->Font->Name + L";}}\n"
  517. "\\f0\\fs" + IntToStr(FResultMemo41->Font->Size * 2) + L" " +
  518. Result +
  519. "}";
  520. FResultMemo41->WordWrap = WordWrap;
  521. FResultMemo41->ScrollBars = WordWrap ? ssVertical : ssBoth;
  522. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  523. UTF8String ResultUtf = Result;
  524. Stream->Write(ResultUtf.c_str(), ResultUtf.Length());
  525. Stream->Position = 0;
  526. FResultMemo41->Perform(WM_VSCROLL, SB_TOP, 0);
  527. FResultMemo41->Lines->LoadFromStream(Stream.get(), TEncoding::UTF8);
  528. }
  529. }
  530. //---------------------------------------------------------------------------
  531. void __fastcall TGenerateUrlDialog::Execute()
  532. {
  533. int Components = WinConfiguration->GenerateUrlComponents;
  534. if (Components < 0)
  535. {
  536. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  537. }
  538. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  539. {
  540. TAutoFlag ChangingFlag(FChanging);
  541. if (IsFileUrl())
  542. {
  543. OptionsPageControl->ActivePage = UrlSheet;
  544. }
  545. else
  546. {
  547. switch (Target)
  548. {
  549. case guctUrl:
  550. OptionsPageControl->ActivePage = UrlSheet;
  551. break;
  552. case guctScript:
  553. OptionsPageControl->ActivePage = ScriptSheet;
  554. break;
  555. case guctAssembly:
  556. OptionsPageControl->ActivePage = AssemblySheet;
  557. break;
  558. default:
  559. DebugFail();
  560. }
  561. }
  562. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  563. {
  564. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  565. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)))
  566. {
  567. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  568. }
  569. }
  570. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  571. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  572. }
  573. UpdateControls();
  574. ShowModal();
  575. // Do not save the selection for files as the "URL" was selected implicitly
  576. if (!IsFileUrl())
  577. {
  578. if (OptionsPageControl->ActivePage == UrlSheet)
  579. {
  580. Target = guctUrl;
  581. }
  582. else if (OptionsPageControl->ActivePage == ScriptSheet)
  583. {
  584. Target = guctScript;
  585. }
  586. else if (OptionsPageControl->ActivePage == AssemblySheet)
  587. {
  588. Target = guctAssembly;
  589. }
  590. else
  591. {
  592. DebugFail();
  593. }
  594. WinConfiguration->GenerateUrlCodeTarget = Target;
  595. }
  596. if (Target == guctUrl)
  597. {
  598. Components = 0;
  599. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  600. {
  601. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  602. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  603. CheckBox->Checked)
  604. {
  605. Components |= CheckBox->Tag;
  606. }
  607. }
  608. WinConfiguration->GenerateUrlComponents = Components;
  609. }
  610. else if (Target == guctScript)
  611. {
  612. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  613. }
  614. else if (Target == guctAssembly)
  615. {
  616. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  617. }
  618. }
  619. //---------------------------------------------------------------------------
  620. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  621. {
  622. UpdateControls();
  623. }
  624. //---------------------------------------------------------------------------
  625. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  626. {
  627. TInstantOperationVisualizer Visualizer;
  628. // Cannot read the text from FResultMemo41->Lines as TRichEdit (as opposite to TMemo)
  629. // breaks wrapped lines
  630. UnicodeString Text = FResultMemo41->Text;
  631. UnicodeString EOL = sLineBreak;
  632. int P = Pos(EOL, Text);
  633. // Trim the EOL of the only string, what CopyToClipbaord(FResultMemo41->Lines) would have done.
  634. // It probably never happens as rich edit does not return EOL on the last line.
  635. if (DebugAlwaysFalse(P == Text.Length() - EOL.Length() + 1))
  636. {
  637. Text.SetLength(Text.Length() - EOL.Length());
  638. }
  639. // Add trailing EOL, if there are multiple lines (see above)
  640. else if ((P > 0) && !EndsStr(EOL, Text))
  641. {
  642. Text += EOL;
  643. }
  644. Text = RtfRemoveHyperlinks(Text);
  645. CopyToClipboard(Text);
  646. }
  647. //---------------------------------------------------------------------------
  648. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  649. {
  650. FormHelp(this);
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TGenerateUrlDialog::WMNCCreate(TWMNCCreate & Message)
  654. {
  655. // bypass TForm::WMNCCreate to prevent disabling "resize"
  656. // (which is done for bsDialog, see comments in CreateParams)
  657. DefaultHandler(&Message);
  658. }
  659. //---------------------------------------------------------------------------
  660. void __fastcall TGenerateUrlDialog::Dispatch(void * AMessage)
  661. {
  662. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  663. if (Message.Msg == WM_NCCREATE)
  664. {
  665. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  666. }
  667. else
  668. {
  669. TForm::Dispatch(AMessage);
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TGenerateUrlDialog::CreateParams(TCreateParams & Params)
  674. {
  675. TForm::CreateParams(Params);
  676. // Allow resizing of the window, even if this is bsDialog.
  677. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  678. // reason receive focus, if window is shown atop non-main window
  679. // (like editor)
  680. Params.Style = Params.Style | WS_THICKFRAME;
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TGenerateUrlDialog::ResultMemoContextPopup(TObject * Sender,
  684. TPoint & MousePos, bool & Handled)
  685. {
  686. MenuPopup(Sender, MousePos, Handled);
  687. }
  688. //---------------------------------------------------------------------------
  689. void __fastcall TGenerateUrlDialog::FormShow(TObject * /*Sender*/)
  690. {
  691. UpdateControls();
  692. }
  693. //---------------------------------------------------------------------------