GenerateUrl.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. UnicodeString __fastcall TGenerateUrlDialog::GenerateUrl()
  244. {
  245. UnicodeString Result;
  246. if (!IsFileUrl())
  247. {
  248. UnicodeString Path = FData->RemoteDirectory;
  249. if (!Path.IsEmpty() && !EndsStr(L"/", Path))
  250. {
  251. Path += L"/";
  252. }
  253. Result = RtfText(GenerateUrl(Path));
  254. }
  255. else
  256. {
  257. for (int Index = 0; Index < FPaths->Count; Index++)
  258. {
  259. UnicodeString Url = GenerateUrl(FPaths->Strings[Index]);
  260. Result += RtfText(Url) + RtfPara;
  261. }
  262. }
  263. return Result;
  264. }
  265. //---------------------------------------------------------------------------
  266. UnicodeString __fastcall TGenerateUrlDialog::GenerateScript(UnicodeString & ScriptDescription)
  267. {
  268. UnicodeString Result;
  269. UnicodeString ExeName = Application->ExeName;
  270. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  271. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  272. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  273. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  274. UnicodeString LogPath = LoadStr(GENERATE_URL_WRITABLE_PATH_TO_LOG) + RtfText(BaseExeName + L".log");
  275. UnicodeString LogParameter =
  276. RtfCommandlineSwitch(LOG_SWITCH, L"logging") + RtfText(L"=") +
  277. RtfScriptPlaceholder(L"\"" + LogPath + L"\"");
  278. UnicodeString IniParameter =
  279. RtfCommandlineSwitch(INI_SWITCH, L"configuration") + RtfText(UnicodeString(L"=") + INI_NUL);
  280. UnicodeString CommandParameter = RtfCommandlineSwitch(COMMAND_SWITCH, L"scripting");
  281. typedef std::vector<UnicodeString> TCommands;
  282. TCommands Commands;
  283. Commands.push_back(RtfScriptCommand(L"open") + L" " + OpenCommand);
  284. Commands.push_back(UnicodeString());
  285. if (FTransfer)
  286. {
  287. UnicodeString TransferCommand;
  288. if (FToRemote)
  289. {
  290. Commands.push_back(RtfScriptCommand(L"lcd") + L" " + RtfText(QuoteStringParam(FSourcePath)));
  291. Commands.push_back(RtfScriptCommand(L"cd") + L" " + RtfText(QuoteStringParam(UnixExcludeTrailingBackslash(FPath))));
  292. TransferCommand = L"put";
  293. }
  294. else
  295. {
  296. Commands.push_back(RtfScriptCommand(L"cd") + L" " + RtfText(QuoteStringParam(FSourcePath)));
  297. Commands.push_back(RtfScriptCommand(L"lcd") + L" " + RtfText(QuoteStringParam(ExcludeTrailingBackslash(FPath))));
  298. TransferCommand = L"get";
  299. }
  300. Commands.push_back(UnicodeString());
  301. UnicodeString TransferCommandLink = ScriptCommandLink(TransferCommand);
  302. UnicodeString TransferCommandArgs;
  303. if (FMove)
  304. {
  305. TransferCommandArgs += RtfSwitch(DELETE_SWITCH, TransferCommandLink);
  306. }
  307. bool NoArgs;
  308. TransferCommandArgs += FCopyParam.GenerateTransferCommandArgs(FCopyParamAttrs, TransferCommandLink, NoArgs);
  309. if (NoArgs)
  310. {
  311. ScriptDescription += LoadStr(GENERATE_URL_COPY_PARAM_SCRIPT_REMAINING) + L"\n";
  312. }
  313. TransferCommand = RtfScriptCommand(TransferCommand) + TransferCommandArgs;
  314. if (FFilesSelected == fsList)
  315. {
  316. for (int Index = 0; Index < FPaths->Count; Index++)
  317. {
  318. Commands.push_back(TransferCommand + L" " + RtfText(QuoteStringParam(EscapeFileMask(FPaths->Strings[Index]))));
  319. }
  320. }
  321. else
  322. {
  323. Commands.push_back(TransferCommand + L" " + RtfText(QuoteStringParam(FSourcePath + L"*")));
  324. }
  325. }
  326. else
  327. {
  328. Commands.push_back(L"# " + CommandPlaceholder1);
  329. Commands.push_back(L"# " + CommandPlaceholder2);
  330. }
  331. Commands.push_back(UnicodeString());
  332. Commands.push_back(RtfScriptCommand(L"exit"));
  333. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  334. {
  335. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  336. {
  337. UnicodeString Command = *I;
  338. if (!Command.IsEmpty())
  339. {
  340. if (Command[1] == L'#')
  341. {
  342. Result += RtfScriptComment(Command);
  343. }
  344. else
  345. {
  346. Result += Command;
  347. }
  348. }
  349. Result += RtfPara;
  350. }
  351. UnicodeString ScriptCommandLine =
  352. FORMAT("\"%s\" /%s=\"%s\" /%s=%s /%s=\"%s\"",
  353. (ExeName, LowerCase(LOG_SWITCH), LogPath, LowerCase(INI_SWITCH), INI_NUL, LowerCase(SCRIPT_SWITCH), LoadStr(GENERATE_URL_PATH_TO_SCRIPT)));
  354. Result +=
  355. RtfPara +
  356. RtfScriptComment(L"# " + LoadStr(GENERATE_URL_SCRIPT_DESC)) + RtfPara +
  357. RtfScriptComment(L"# " + ScriptCommandLine) + RtfPara;
  358. }
  359. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  360. {
  361. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  362. Result =
  363. RtfScriptPlaceholder(L"@echo off") + RtfPara +
  364. RtfPara +
  365. RtfText(L"\"" + ComExeName + "\" ^") + RtfPara +
  366. RtfText(L" ") + LogParameter + L" " + IniParameter + RtfText(L" ^") + RtfPara +
  367. RtfText(L" ") + CommandParameter;
  368. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  369. {
  370. UnicodeString Command = *I;
  371. if (!Command.IsEmpty())
  372. {
  373. Result +=
  374. RtfText(L" ^") + RtfPara +
  375. RtfText(L" \"");
  376. if (Command[1] == L'#')
  377. {
  378. Command.Delete(1, 1);
  379. Result += RtfScriptPlaceholder(Command.TrimLeft());
  380. }
  381. else
  382. {
  383. Result += RtfEscapeParam(ReplaceStr(Command, L"%", L"%%"));
  384. }
  385. Result += L"\"";
  386. }
  387. }
  388. Result +=
  389. RtfPara +
  390. RtfPara +
  391. RtfKeyword(L"set") + RtfText(L" WINSCP_RESULT=%ERRORLEVEL%") + RtfPara +
  392. RtfKeyword(L"if") + RtfText(L" %WINSCP_RESULT% ") + RtfKeyword(L"equ") + RtfText(L" 0 (") + RtfPara +
  393. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Success") + RtfPara +
  394. RtfText(L") ") + RtfKeyword(L"else") + RtfText(L" (") + RtfPara +
  395. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Error") + RtfPara +
  396. RtfText(L")") + RtfPara +
  397. RtfPara +
  398. RtfKeyword(L"exit") + RtfText(L" /b %WINSCP_RESULT%") + RtfPara;
  399. }
  400. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  401. {
  402. Result =
  403. LogParameter + L" " +
  404. IniParameter + L" " +
  405. CommandParameter;
  406. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  407. {
  408. UnicodeString Command = *I;
  409. if (!Command.IsEmpty())
  410. {
  411. Result += RtfText(L" \"");
  412. if (Command[1] == L'#')
  413. {
  414. Command.Delete(1, 1);
  415. Result += RtfScriptPlaceholder(Command.TrimLeft());
  416. }
  417. else
  418. {
  419. Result += RtfEscapeParam(Command);
  420. }
  421. Result += L"\"";
  422. }
  423. }
  424. }
  425. return Result;
  426. }
  427. //---------------------------------------------------------------------------
  428. UnicodeString __fastcall TGenerateUrlDialog::GenerateAssemblyCode()
  429. {
  430. TAssemblyLanguage Language = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  431. UnicodeString Head;
  432. UnicodeString Tail;
  433. int Indent;
  434. FData->GenerateAssemblyCode(Language, Head, Tail, Indent);
  435. UnicodeString Indentation = UnicodeString::StringOfChar(L' ', Indent);
  436. UnicodeString Result =
  437. Head +
  438. Indentation + AssemblyCommentLine(Language, LoadStr(GENERATE_URL_YOUR_CODE)) +
  439. Tail;
  440. return Result;
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TGenerateUrlDialog::UpdateControls()
  444. {
  445. if (!FChanging)
  446. {
  447. int CaptionId;
  448. if (FTransfer)
  449. {
  450. CaptionId = GENERATE_URL_TRANSFER_TITLE;
  451. }
  452. else
  453. {
  454. CaptionId = IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE;
  455. }
  456. Caption = LoadStr(CaptionId);
  457. UrlSheet->TabVisible = !FTransfer;
  458. ScriptSheet->TabVisible = !IsFileUrl();
  459. AssemblySheet->TabVisible = !IsFileUrl();
  460. bool HostKeyUnknown = FData->UsesSsh && FData->HostKey.IsEmpty();
  461. UnicodeString ResultGroupCaption;
  462. if (OptionsPageControl->ActivePage == UrlSheet)
  463. {
  464. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  465. }
  466. else if (OptionsPageControl->ActivePage == ScriptSheet)
  467. {
  468. if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  469. {
  470. ResultGroupCaption = LoadStr(GENERATE_URL_COMMANDLINE_LABEL);
  471. }
  472. else
  473. {
  474. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  475. }
  476. }
  477. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  478. {
  479. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  480. UnicodeString AssemblyDescription;
  481. if (HostKeyUnknown)
  482. {
  483. AssemblyDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  484. }
  485. AssemblyDescriptionLabel->Caption = AssemblyDescription;
  486. }
  487. ResultGroup->Caption = ResultGroupCaption;
  488. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  489. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  490. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  491. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  492. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  493. EnableControl(SaveExtensionCheck, !IsFileUrl());
  494. UnicodeString Result;
  495. bool WordWrap = false; // shut up
  496. bool FixedWidth = false; // shut up
  497. if (OptionsPageControl->ActivePage == UrlSheet)
  498. {
  499. Result = GenerateUrl();
  500. WordWrap = true;
  501. FixedWidth = false;
  502. }
  503. else if (OptionsPageControl->ActivePage == ScriptSheet)
  504. {
  505. UnicodeString ScriptDescription;
  506. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  507. {
  508. WordWrap = false;
  509. FixedWidth = true;
  510. }
  511. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  512. {
  513. WordWrap = false;
  514. FixedWidth = true;
  515. }
  516. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  517. {
  518. WordWrap = true;
  519. FixedWidth = false;
  520. ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (Application->ExeName)))) + L"\n";
  521. }
  522. if (HostKeyUnknown)
  523. {
  524. ScriptDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  525. }
  526. Result = GenerateScript(ScriptDescription);
  527. ScriptDescriptionLabel->Caption = ScriptDescription;
  528. }
  529. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  530. {
  531. Result = GenerateAssemblyCode();
  532. WordWrap = false;
  533. FixedWidth = true;
  534. }
  535. if (FixedWidth)
  536. {
  537. FResultMemo41->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  538. }
  539. else
  540. {
  541. FResultMemo41->ParentFont = true;
  542. }
  543. Result =
  544. L"{\\rtf1\n"
  545. "{\\colortbl ;" +
  546. // The same RGB as on wiki
  547. RtfColorEntry(0x010101) + // near-black fake color to be used with no-style link to ovreride the default blue underline
  548. RtfColorEntry(0x008000) + // code comment (green)
  549. RtfColorEntry(0x008080) + // class (teal)
  550. RtfColorEntry(0x800000) + // string (maroon)
  551. RtfColorEntry(0x0000FF) + // keyword (blue)
  552. RtfColorEntry(0x993333) + // command-line argument (reddish)
  553. RtfColorEntry(0x808080) + // script command (gray)
  554. L"}\n"
  555. "{\\fonttbl{\\f0\\fnil\\fcharset0 " + FResultMemo41->Font->Name + L";}}\n"
  556. "\\f0\\fs" + IntToStr(FResultMemo41->Font->Size * 2) + L" " +
  557. Result +
  558. "}";
  559. FResultMemo41->WordWrap = WordWrap;
  560. FResultMemo41->ScrollBars = WordWrap ? ssVertical : ssBoth;
  561. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  562. UTF8String ResultUtf = Result;
  563. Stream->Write(ResultUtf.c_str(), ResultUtf.Length());
  564. Stream->Position = 0;
  565. FResultMemo41->Perform(WM_VSCROLL, SB_TOP, 0);
  566. FResultMemo41->Lines->LoadFromStream(Stream.get(), TEncoding::UTF8);
  567. }
  568. }
  569. //---------------------------------------------------------------------------
  570. void __fastcall TGenerateUrlDialog::Execute()
  571. {
  572. int Components = WinConfiguration->GenerateUrlComponents;
  573. if (Components < 0)
  574. {
  575. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  576. }
  577. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  578. {
  579. TAutoFlag ChangingFlag(FChanging);
  580. if (IsFileUrl())
  581. {
  582. OptionsPageControl->ActivePage = UrlSheet;
  583. }
  584. else
  585. {
  586. switch (Target)
  587. {
  588. case guctUrl:
  589. OptionsPageControl->ActivePage = UrlSheet;
  590. break;
  591. case guctScript:
  592. OptionsPageControl->ActivePage = ScriptSheet;
  593. break;
  594. case guctAssembly:
  595. OptionsPageControl->ActivePage = AssemblySheet;
  596. break;
  597. default:
  598. DebugFail();
  599. }
  600. }
  601. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  602. {
  603. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  604. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)))
  605. {
  606. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  607. }
  608. }
  609. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  610. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  611. }
  612. UpdateControls();
  613. ShowModal();
  614. // Do not save the selection for files as the "URL" was selected implicitly
  615. if (!IsFileUrl())
  616. {
  617. if (OptionsPageControl->ActivePage == UrlSheet)
  618. {
  619. Target = guctUrl;
  620. }
  621. else if (OptionsPageControl->ActivePage == ScriptSheet)
  622. {
  623. Target = guctScript;
  624. }
  625. else if (OptionsPageControl->ActivePage == AssemblySheet)
  626. {
  627. Target = guctAssembly;
  628. }
  629. else
  630. {
  631. DebugFail();
  632. }
  633. WinConfiguration->GenerateUrlCodeTarget = Target;
  634. }
  635. if (Target == guctUrl)
  636. {
  637. Components = 0;
  638. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  639. {
  640. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  641. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  642. CheckBox->Checked)
  643. {
  644. Components |= CheckBox->Tag;
  645. }
  646. }
  647. WinConfiguration->GenerateUrlComponents = Components;
  648. }
  649. else if (Target == guctScript)
  650. {
  651. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  652. }
  653. else if (Target == guctAssembly)
  654. {
  655. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  656. }
  657. }
  658. //---------------------------------------------------------------------------
  659. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  660. {
  661. UpdateControls();
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  665. {
  666. TInstantOperationVisualizer Visualizer;
  667. // Cannot read the text from FResultMemo41->Lines as TRichEdit (as opposite to TMemo)
  668. // breaks wrapped lines
  669. UnicodeString Text = FResultMemo41->Text;
  670. UnicodeString EOL = sLineBreak;
  671. int P = Pos(EOL, Text);
  672. // Trim the EOL of the only string, what CopyToClipbaord(FResultMemo41->Lines) would have done.
  673. // It probably never happens as rich edit does not return EOL on the last line.
  674. if (DebugAlwaysFalse(P == Text.Length() - EOL.Length() + 1))
  675. {
  676. Text.SetLength(Text.Length() - EOL.Length());
  677. }
  678. // Add trailing EOL, if there are multiple lines (see above)
  679. else if ((P > 0) && !EndsStr(EOL, Text))
  680. {
  681. Text += EOL;
  682. }
  683. Text = RtfRemoveHyperlinks(Text);
  684. CopyToClipboard(Text);
  685. }
  686. //---------------------------------------------------------------------------
  687. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  688. {
  689. FormHelp(this);
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TGenerateUrlDialog::WMNCCreate(TWMNCCreate & Message)
  693. {
  694. // bypass TForm::WMNCCreate to prevent disabling "resize"
  695. // (which is done for bsDialog, see comments in CreateParams)
  696. DefaultHandler(&Message);
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TGenerateUrlDialog::Dispatch(void * AMessage)
  700. {
  701. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  702. if (Message.Msg == WM_NCCREATE)
  703. {
  704. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  705. }
  706. else
  707. {
  708. TForm::Dispatch(AMessage);
  709. }
  710. }
  711. //---------------------------------------------------------------------------
  712. void __fastcall TGenerateUrlDialog::CreateParams(TCreateParams & Params)
  713. {
  714. TForm::CreateParams(Params);
  715. // Allow resizing of the window, even if this is bsDialog.
  716. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  717. // reason receive focus, if window is shown atop non-main window
  718. // (like editor)
  719. Params.Style = Params.Style | WS_THICKFRAME;
  720. }
  721. //---------------------------------------------------------------------------
  722. void __fastcall TGenerateUrlDialog::ResultMemoContextPopup(TObject * Sender,
  723. TPoint & MousePos, bool & Handled)
  724. {
  725. MenuPopup(Sender, MousePos, Handled);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TGenerateUrlDialog::FormShow(TObject * /*Sender*/)
  729. {
  730. UpdateControls();
  731. }
  732. //---------------------------------------------------------------------------