GenerateUrl.cpp 33 KB

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