GenerateUrl.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. UnicodeString __fastcall TGenerateUrlDialog::GenerateScript(UnicodeString & ScriptDescription)
  281. {
  282. UnicodeString Result;
  283. UnicodeString ExeName = Application->ExeName;
  284. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  285. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  286. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  287. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  288. UnicodeString LogPath = LoadStr(GENERATE_URL_WRITABLE_PATH_TO_LOG) + RtfText(BaseExeName + L".log");
  289. UnicodeString LogParameter =
  290. RtfCommandlineSwitch(LOG_SWITCH, L"logging") + RtfText(L"=") +
  291. RtfScriptPlaceholder(L"\"" + LogPath + L"\"");
  292. UnicodeString IniParameter =
  293. RtfCommandlineSwitch(INI_SWITCH, L"configuration") + RtfText(UnicodeString(L"=") + INI_NUL);
  294. UnicodeString CommandParameter = RtfCommandlineSwitch(COMMAND_SWITCH, L"scripting");
  295. typedef std::vector<UnicodeString> TCommands;
  296. TCommands Commands;
  297. Commands.push_back(RtfScriptCommand(L"open") + L" " + OpenCommand);
  298. Commands.push_back(UnicodeString());
  299. if (FTransfer)
  300. {
  301. UnicodeString TransferCommand;
  302. if (FToRemote)
  303. {
  304. Commands.push_back(RtfScriptCommand(L"lcd") + L" " + RtfText(QuoteStringParam(FSourcePath)));
  305. Commands.push_back(RtfScriptCommand(L"cd") + L" " + RtfText(QuoteStringParam(UnixExcludeTrailingBackslash(FPath))));
  306. TransferCommand = L"put";
  307. }
  308. else
  309. {
  310. Commands.push_back(RtfScriptCommand(L"cd") + L" " + RtfText(QuoteStringParam(FSourcePath)));
  311. Commands.push_back(RtfScriptCommand(L"lcd") + L" " + RtfText(QuoteStringParam(ExcludeTrailingBackslash(FPath))));
  312. TransferCommand = L"get";
  313. }
  314. Commands.push_back(UnicodeString());
  315. UnicodeString TransferCommandLink = ScriptCommandLink(TransferCommand);
  316. UnicodeString TransferCommandArgs;
  317. if (FMove)
  318. {
  319. TransferCommandArgs += RtfSwitch(DELETE_SWITCH, TransferCommandLink);
  320. }
  321. bool NoArgs;
  322. TransferCommandArgs += FCopyParam.GenerateTransferCommandArgs(FCopyParamAttrs, TransferCommandLink, NoArgs);
  323. if (NoArgs)
  324. {
  325. ScriptDescription += LoadStr(GENERATE_URL_COPY_PARAM_SCRIPT_REMAINING) + L"\n";
  326. }
  327. AddSampleDescription(ScriptDescription);
  328. TransferCommand = RtfScriptCommand(TransferCommand) + TransferCommandArgs;
  329. if (FFilesSelected == fsList)
  330. {
  331. for (int Index = 0; Index < FPaths->Count; Index++)
  332. {
  333. UnicodeString Path = ExtractFileName(FPaths->Strings[Index], !FToRemote);
  334. if (!FToRemote)
  335. {
  336. Path = EscapeFileMask(Path);
  337. }
  338. Commands.push_back(TransferCommand + L" " + RtfText(QuoteStringParam(Path)));
  339. }
  340. }
  341. else
  342. {
  343. Commands.push_back(TransferCommand + L" " + RtfText(AllFilesMask));
  344. }
  345. }
  346. else
  347. {
  348. Commands.push_back(L"# " + CommandPlaceholder1);
  349. Commands.push_back(L"# " + CommandPlaceholder2);
  350. }
  351. Commands.push_back(UnicodeString());
  352. Commands.push_back(RtfScriptCommand(L"exit"));
  353. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  354. {
  355. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  356. {
  357. UnicodeString Command = *I;
  358. if (!Command.IsEmpty())
  359. {
  360. if (Command[1] == L'#')
  361. {
  362. Result += RtfScriptComment(Command);
  363. }
  364. else
  365. {
  366. Result += Command;
  367. }
  368. }
  369. Result += RtfPara;
  370. }
  371. UnicodeString ScriptCommandLine =
  372. FORMAT("\"%s\" /%s=\"%s\" /%s=%s /%s=\"%s\"",
  373. (ExeName, LowerCase(LOG_SWITCH), LogPath, LowerCase(INI_SWITCH), INI_NUL, LowerCase(SCRIPT_SWITCH), LoadStr(GENERATE_URL_PATH_TO_SCRIPT)));
  374. Result +=
  375. RtfPara +
  376. RtfScriptComment(L"# " + LoadStr(GENERATE_URL_SCRIPT_DESC)) + RtfPara +
  377. RtfScriptComment(L"# " + ScriptCommandLine) + RtfPara;
  378. }
  379. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  380. {
  381. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  382. Result =
  383. RtfScriptPlaceholder(L"@echo off") + RtfPara +
  384. RtfPara +
  385. RtfText(L"\"" + ComExeName + "\" ^") + RtfPara +
  386. RtfText(L" ") + LogParameter + L" " + IniParameter + RtfText(L" ^") + RtfPara +
  387. RtfText(L" ") + CommandParameter;
  388. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  389. {
  390. UnicodeString Command = *I;
  391. if (!Command.IsEmpty())
  392. {
  393. Result +=
  394. RtfText(L" ^") + RtfPara +
  395. RtfText(L" \"");
  396. if (Command[1] == L'#')
  397. {
  398. Command.Delete(1, 1);
  399. Result += RtfScriptPlaceholder(Command.TrimLeft());
  400. }
  401. else
  402. {
  403. Result += RtfEscapeParam(ReplaceStr(Command, L"%", L"%%"));
  404. }
  405. Result += L"\"";
  406. }
  407. }
  408. Result +=
  409. RtfPara +
  410. RtfPara +
  411. RtfKeyword(L"set") + RtfText(L" WINSCP_RESULT=%ERRORLEVEL%") + RtfPara +
  412. RtfKeyword(L"if") + RtfText(L" %WINSCP_RESULT% ") + RtfKeyword(L"equ") + RtfText(L" 0 (") + RtfPara +
  413. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Success") + RtfPara +
  414. RtfText(L") ") + RtfKeyword(L"else") + RtfText(L" (") + RtfPara +
  415. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Error") + RtfPara +
  416. RtfText(L")") + RtfPara +
  417. RtfPara +
  418. RtfKeyword(L"exit") + RtfText(L" /b %WINSCP_RESULT%") + RtfPara;
  419. }
  420. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  421. {
  422. Result =
  423. LogParameter + L" " +
  424. IniParameter + L" " +
  425. CommandParameter;
  426. for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
  427. {
  428. UnicodeString Command = *I;
  429. if (!Command.IsEmpty())
  430. {
  431. Result += RtfText(L" \"");
  432. if (Command[1] == L'#')
  433. {
  434. Command.Delete(1, 1);
  435. Result += RtfScriptPlaceholder(Command.TrimLeft());
  436. }
  437. else
  438. {
  439. Result += RtfEscapeParam(Command);
  440. }
  441. Result += L"\"";
  442. }
  443. }
  444. }
  445. return Result;
  446. }
  447. //---------------------------------------------------------------------------
  448. UnicodeString __fastcall TGenerateUrlDialog::GenerateAssemblyCode(UnicodeString & AssemblyDescription)
  449. {
  450. TAssemblyLanguage Language = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  451. UnicodeString Head;
  452. UnicodeString Tail;
  453. int Indent;
  454. FData->GenerateAssemblyCode(Language, Head, Tail, Indent);
  455. UnicodeString Result = Head;
  456. UnicodeString Code;
  457. if (FTransfer)
  458. {
  459. bool NoCodeProperties;
  460. UnicodeString CopyParamProperties =
  461. FCopyParam.GenerateAssemblyCode(Language, FCopyParamAttrs, NoCodeProperties);
  462. if (NoCodeProperties)
  463. {
  464. AssemblyDescription += LoadStr(GENERATE_URL_COPY_PARAM_CODE_REMAINING) + L"\n";
  465. }
  466. bool HasTransferOptions = !CopyParamProperties.IsEmpty();
  467. if (HasTransferOptions)
  468. {
  469. Code +=
  470. AssemblyCommentLine(Language, LoadStr(GENERATE_URL_COPY_PARAM)) +
  471. AssemblyNewClassInstanceStart(Language, TransferOptionsClassName, false) +
  472. CopyParamProperties +
  473. AssemblyNewClassInstanceEnd(Language, false) +
  474. RtfPara;
  475. }
  476. Code += AssemblyCommentLine(Language, LoadStr(GENERATE_URL_TRANSFER_FILES));
  477. AddSampleDescription(AssemblyDescription);
  478. UnicodeString DestPath = FPath;
  479. UnicodeString TransferMethodName;
  480. if (FToRemote)
  481. {
  482. TransferMethodName = L"PutFiles";
  483. DestPath = UnixIncludeTrailingBackslash(DestPath);
  484. }
  485. else
  486. {
  487. TransferMethodName = L"GetFiles";
  488. DestPath = IncludeTrailingBackslash(DestPath);
  489. }
  490. DestPath += NoOpOperationMask;
  491. UnicodeString DestPathVariableName = AssemblyVariableName(Language, L"remotePath");
  492. UnicodeString StatementSeparator = AssemblyStatementSeparator(Language);
  493. UnicodeString DestPathString = AssemblyString(Language, DestPath);
  494. UnicodeString DestPathCode;
  495. if ((FFilesSelected != fsList) || (FPaths->Count == 1))
  496. {
  497. DestPathCode = DestPathString;
  498. }
  499. else
  500. {
  501. switch (Language)
  502. {
  503. case alCSharp:
  504. Code += RtfKeyword(L"const") + L" " + RtfKeyword(L"string") + L" " + DestPathVariableName;
  505. break;
  506. case alVBNET:
  507. Code += RtfKeyword(L"Const") + L" " + DestPathVariableName;
  508. break;
  509. case alPowerShell:
  510. Code += DestPathVariableName;
  511. break;
  512. }
  513. Code += L" = " + DestPathString + StatementSeparator + RtfPara;
  514. DestPathCode = DestPathVariableName;
  515. }
  516. UnicodeString TransferMethodCallStart =
  517. AssemblyVariableName(Language, SessionClassName) + L"." +
  518. RtfLibraryMethod(SessionClassName, TransferMethodName, false) + L"(";
  519. const UnicodeString ParameterSeparator = L", ";
  520. UnicodeString TransferMethodCallEnd = ParameterSeparator + DestPathCode;
  521. if (FMove || HasTransferOptions)
  522. {
  523. TransferMethodCallEnd += ParameterSeparator + AssemblyBoolean(Language, FMove);
  524. }
  525. if (HasTransferOptions)
  526. {
  527. TransferMethodCallEnd += ParameterSeparator + AssemblyVariableName(Language, TransferOptionsClassName);
  528. }
  529. TransferMethodCallEnd +=
  530. L")." + RtfLibraryMethod(L"OperationResultBase", L"Check", true) + L"()" +
  531. StatementSeparator + RtfPara;
  532. if (FFilesSelected == fsList)
  533. {
  534. for (int Index = 0; Index < FPaths->Count; Index++)
  535. {
  536. UnicodeString FileName = FPaths->Strings[Index];
  537. UnicodeString Path;
  538. if (!FToRemote)
  539. {
  540. Path = UnixIncludeTrailingBackslash(FSourcePath) + FileName;
  541. }
  542. else
  543. {
  544. Path = IncludeTrailingBackslash(FSourcePath) + FileName;
  545. }
  546. UnicodeString PathCode = AssemblyString(Language, Path);
  547. if (!FToRemote && (FileName != EscapeFileMask(FileName)))
  548. {
  549. PathCode =
  550. AssemblyVariableName(Language, SessionClassName) + L"." +
  551. RtfLibraryMethod(SessionClassName, L"EscapeFileMask", false) + L"(" + PathCode + L")";
  552. }
  553. Code += TransferMethodCallStart + PathCode + TransferMethodCallEnd;
  554. }
  555. }
  556. else
  557. {
  558. UnicodeString SourcePath = FSourcePath;
  559. if (FToRemote)
  560. {
  561. SourcePath = IncludeTrailingBackslash(SourcePath);
  562. }
  563. else
  564. {
  565. SourcePath = UnixIncludeTrailingBackslash(SourcePath);
  566. }
  567. SourcePath += AllFilesMask;
  568. Code += TransferMethodCallStart + AssemblyString(Language, SourcePath) + TransferMethodCallEnd;
  569. }
  570. }
  571. else
  572. {
  573. Code = AssemblyCommentLine(Language, LoadStr(GENERATE_URL_YOUR_CODE));
  574. }
  575. UnicodeString Indentation = UnicodeString::StringOfChar(L' ', Indent);
  576. Code = Indentation + ReplaceStr(Code, RtfPara, RtfPara + Indentation);
  577. if (DebugAlwaysTrue(Code.SubString(Code.Length() - Indentation.Length() + 1, Indentation.Length()) == Indentation))
  578. {
  579. Code.SetLength(Code.Length() - Indentation.Length());
  580. }
  581. Result += Code;
  582. Result += Tail;
  583. return Result;
  584. }
  585. //---------------------------------------------------------------------------
  586. void __fastcall TGenerateUrlDialog::UpdateControls()
  587. {
  588. if (!FChanging)
  589. {
  590. int CaptionId;
  591. if (FTransfer)
  592. {
  593. CaptionId = GENERATE_URL_TRANSFER_TITLE;
  594. }
  595. else
  596. {
  597. CaptionId = IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE;
  598. }
  599. Caption = LoadStr(CaptionId);
  600. UrlSheet->TabVisible = !FTransfer;
  601. ScriptSheet->TabVisible = !IsFileUrl();
  602. AssemblySheet->TabVisible = !IsFileUrl();
  603. bool HostKeyUnknown = FData->UsesSsh && FData->HostKey.IsEmpty();
  604. UnicodeString ResultGroupCaption;
  605. if (OptionsPageControl->ActivePage == UrlSheet)
  606. {
  607. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  608. }
  609. else if (OptionsPageControl->ActivePage == ScriptSheet)
  610. {
  611. if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  612. {
  613. ResultGroupCaption = LoadStr(GENERATE_URL_COMMANDLINE_LABEL);
  614. }
  615. else
  616. {
  617. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  618. }
  619. }
  620. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  621. {
  622. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  623. }
  624. ResultGroup->Caption = ResultGroupCaption;
  625. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  626. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  627. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  628. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  629. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  630. EnableControl(SaveExtensionCheck, !IsFileUrl());
  631. UnicodeString Result;
  632. bool WordWrap = false; // shut up
  633. bool FixedWidth = false; // shut up
  634. if (OptionsPageControl->ActivePage == UrlSheet)
  635. {
  636. Result = GenerateUrl();
  637. WordWrap = true;
  638. FixedWidth = false;
  639. }
  640. else if (OptionsPageControl->ActivePage == ScriptSheet)
  641. {
  642. UnicodeString ScriptDescription;
  643. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  644. {
  645. WordWrap = false;
  646. FixedWidth = true;
  647. }
  648. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  649. {
  650. WordWrap = false;
  651. FixedWidth = true;
  652. }
  653. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  654. {
  655. WordWrap = true;
  656. FixedWidth = false;
  657. ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (Application->ExeName)))) + L"\n";
  658. }
  659. if (HostKeyUnknown)
  660. {
  661. ScriptDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  662. }
  663. Result = GenerateScript(ScriptDescription);
  664. ScriptDescriptionLabel->Caption = ScriptDescription;
  665. }
  666. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  667. {
  668. UnicodeString AssemblyDescription;
  669. if (HostKeyUnknown)
  670. {
  671. AssemblyDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  672. }
  673. Result = GenerateAssemblyCode(AssemblyDescription);
  674. AssemblyDescriptionLabel->Caption = AssemblyDescription;
  675. WordWrap = false;
  676. FixedWidth = true;
  677. }
  678. if (FixedWidth)
  679. {
  680. FResultMemo41->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  681. }
  682. else
  683. {
  684. FResultMemo41->ParentFont = true;
  685. }
  686. Result =
  687. L"{\\rtf1\n"
  688. "{\\colortbl ;" +
  689. // The same RGB as on wiki
  690. RtfColorEntry(0x010101) + // near-black fake color to be used with no-style link to ovreride the default blue underline
  691. RtfColorEntry(0x008000) + // code comment (green)
  692. RtfColorEntry(0x008080) + // class (teal)
  693. RtfColorEntry(0x800000) + // string (maroon)
  694. RtfColorEntry(0x0000FF) + // keyword (blue)
  695. RtfColorEntry(0x993333) + // command-line argument (reddish)
  696. RtfColorEntry(0x808080) + // script command (gray)
  697. L"}\n"
  698. "{\\fonttbl{\\f0\\fnil\\fcharset0 " + FResultMemo41->Font->Name + L";}}\n"
  699. "\\f0\\fs" + IntToStr(FResultMemo41->Font->Size * 2) + L" " +
  700. Result +
  701. "}";
  702. FResultMemo41->WordWrap = WordWrap;
  703. FResultMemo41->ScrollBars = WordWrap ? ssVertical : ssBoth;
  704. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  705. UTF8String ResultUtf = Result;
  706. Stream->Write(ResultUtf.c_str(), ResultUtf.Length());
  707. Stream->Position = 0;
  708. FResultMemo41->Perform(WM_VSCROLL, SB_TOP, 0);
  709. FResultMemo41->Lines->LoadFromStream(Stream.get(), TEncoding::UTF8);
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TGenerateUrlDialog::Execute()
  714. {
  715. int Components = WinConfiguration->GenerateUrlComponents;
  716. if (Components < 0)
  717. {
  718. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  719. }
  720. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  721. {
  722. TAutoFlag ChangingFlag(FChanging);
  723. if (IsFileUrl())
  724. {
  725. OptionsPageControl->ActivePage = UrlSheet;
  726. }
  727. else
  728. {
  729. switch (Target)
  730. {
  731. case guctUrl:
  732. OptionsPageControl->ActivePage = UrlSheet;
  733. break;
  734. case guctScript:
  735. OptionsPageControl->ActivePage = ScriptSheet;
  736. break;
  737. case guctAssembly:
  738. OptionsPageControl->ActivePage = AssemblySheet;
  739. break;
  740. default:
  741. DebugFail();
  742. }
  743. }
  744. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  745. {
  746. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  747. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)))
  748. {
  749. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  750. }
  751. }
  752. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  753. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  754. }
  755. UpdateControls();
  756. ShowModal();
  757. // Do not save the selection for files as the "URL" was selected implicitly
  758. if (!IsFileUrl())
  759. {
  760. if (OptionsPageControl->ActivePage == UrlSheet)
  761. {
  762. Target = guctUrl;
  763. }
  764. else if (OptionsPageControl->ActivePage == ScriptSheet)
  765. {
  766. Target = guctScript;
  767. }
  768. else if (OptionsPageControl->ActivePage == AssemblySheet)
  769. {
  770. Target = guctAssembly;
  771. }
  772. else
  773. {
  774. DebugFail();
  775. }
  776. WinConfiguration->GenerateUrlCodeTarget = Target;
  777. }
  778. if (Target == guctUrl)
  779. {
  780. Components = 0;
  781. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  782. {
  783. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  784. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  785. CheckBox->Checked)
  786. {
  787. Components |= CheckBox->Tag;
  788. }
  789. }
  790. WinConfiguration->GenerateUrlComponents = Components;
  791. }
  792. else if (Target == guctScript)
  793. {
  794. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  795. }
  796. else if (Target == guctAssembly)
  797. {
  798. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  799. }
  800. }
  801. //---------------------------------------------------------------------------
  802. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  803. {
  804. UpdateControls();
  805. }
  806. //---------------------------------------------------------------------------
  807. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  808. {
  809. TInstantOperationVisualizer Visualizer;
  810. // Cannot read the text from FResultMemo41->Lines as TRichEdit (as opposite to TMemo)
  811. // breaks wrapped lines
  812. UnicodeString Text = FResultMemo41->Text;
  813. UnicodeString EOL = sLineBreak;
  814. int P = Pos(EOL, Text);
  815. // Trim the EOL of the only string, what CopyToClipbaord(FResultMemo41->Lines) would have done.
  816. // It probably never happens as rich edit does not return EOL on the last line.
  817. if (DebugAlwaysFalse(P == Text.Length() - EOL.Length() + 1))
  818. {
  819. Text.SetLength(Text.Length() - EOL.Length());
  820. }
  821. // Add trailing EOL, if there are multiple lines (see above)
  822. else if ((P > 0) && !EndsStr(EOL, Text))
  823. {
  824. Text += EOL;
  825. }
  826. Text = RtfRemoveHyperlinks(Text);
  827. CopyToClipboard(Text);
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  831. {
  832. FormHelp(this);
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TGenerateUrlDialog::WMNCCreate(TWMNCCreate & Message)
  836. {
  837. // bypass TForm::WMNCCreate to prevent disabling "resize"
  838. // (which is done for bsDialog, see comments in CreateParams)
  839. DefaultHandler(&Message);
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TGenerateUrlDialog::Dispatch(void * AMessage)
  843. {
  844. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  845. if (Message.Msg == WM_NCCREATE)
  846. {
  847. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  848. }
  849. else
  850. {
  851. TForm::Dispatch(AMessage);
  852. }
  853. }
  854. //---------------------------------------------------------------------------
  855. void __fastcall TGenerateUrlDialog::CreateParams(TCreateParams & Params)
  856. {
  857. TForm::CreateParams(Params);
  858. // Allow resizing of the window, even if this is bsDialog.
  859. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  860. // reason receive focus, if window is shown atop non-main window
  861. // (like editor)
  862. Params.Style = Params.Style | WS_THICKFRAME;
  863. }
  864. //---------------------------------------------------------------------------
  865. void __fastcall TGenerateUrlDialog::ResultMemoContextPopup(TObject * Sender,
  866. TPoint & MousePos, bool & Handled)
  867. {
  868. MenuPopup(Sender, MousePos, Handled);
  869. }
  870. //---------------------------------------------------------------------------
  871. void __fastcall TGenerateUrlDialog::FormShow(TObject * /*Sender*/)
  872. {
  873. UpdateControls();
  874. }
  875. //---------------------------------------------------------------------------