GenerateUrl.cpp 32 KB

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