GenerateUrl.cpp 32 KB

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