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