GenerateUrl.cpp 31 KB

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