GenerateUrl.cpp 32 KB

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