GenerateUrl.cpp 31 KB

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