GenerateUrl.cpp 33 KB

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