GenerateUrl.cpp 31 KB

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