瀏覽代碼

Separating head and tail session code generation to allow inserting transfer code

Source commit: ca54ddb73fb00a44fa0e5bf7a65f7a1c29a79b41
Martin Prikryl 9 年之前
父節點
當前提交
aeec70feb5

+ 26 - 0
source/core/Common.cpp

@@ -3085,3 +3085,29 @@ UnicodeString __fastcall RtfEscapeParam(UnicodeString Param)
 
   return Param;
 }
+//---------------------------------------------------------------------
+static UnicodeString __fastcall RtfCodeComment(const UnicodeString & Text)
+{
+  return RtfColorItalicText(2, Text);
+}
+//---------------------------------------------------------------------
+UnicodeString __fastcall AssemblyCommentLine(TAssemblyLanguage Language, const UnicodeString & Text)
+{
+  UnicodeString Prefix;
+  switch (Language)
+  {
+    case alCSharp:
+      Prefix = L"//";
+      break;
+
+    case alVBNET:
+      Prefix = L"'";
+      break;
+
+    case alPowerShell:
+      Prefix = L"#";
+      break;
+  }
+
+  return RtfCodeComment(Prefix + L" " + Text) + RtfPara;
+}

+ 2 - 0
source/core/Common.h

@@ -202,6 +202,7 @@ MethodT __fastcall MakeMethod(void * Data, void * Code)
   return Method;
 }
 //---------------------------------------------------------------------------
+enum TAssemblyLanguage { alCSharp, alVBNET, alPowerShell };
 extern const UnicodeString RtfPara;
 //---------------------------------------------------------------------
 UnicodeString __fastcall RtfText(const UnicodeString & Text);
@@ -220,6 +221,7 @@ UnicodeString __fastcall RtfSwitch(const UnicodeString & Name, const UnicodeStri
 UnicodeString __fastcall RtfEscapeParam(UnicodeString Param);
 UnicodeString __fastcall RtfRemoveHyperlinks(UnicodeString Text);
 UnicodeString __fastcall ScriptCommandLink(const UnicodeString & Command);
+UnicodeString __fastcall AssemblyCommentLine(TAssemblyLanguage Language, const UnicodeString & Text);
 //---------------------------------------------------------------------------
 #include "Global.h"
 //---------------------------------------------------------------------------

+ 0 - 1
source/core/Configuration.h

@@ -16,7 +16,6 @@
 extern const wchar_t * AutoSwitchNames;
 extern const wchar_t * NotAutoSwitchNames;
 enum TAutoSwitch { asOn, asOff, asAuto };
-enum TAssemblyLanguage { alCSharp, alVBNET, alPowerShell };
 //---------------------------------------------------------------------------
 class TStoredSessionList;
 //---------------------------------------------------------------------------

+ 45 - 52
source/core/SessionData.cpp

@@ -2559,11 +2559,6 @@ void __fastcall TSessionData::LookupLastFingerprint()
   }
 }
 //---------------------------------------------------------------------
-static UnicodeString __fastcall RtfCodeComment(const UnicodeString & Text)
-{
-  return RtfColorItalicText(2, Text);
-}
-//---------------------------------------------------------------------
 static UnicodeString __fastcall RtfClass(const UnicodeString & Text)
 {
   return RtfColorText(3, Text);
@@ -2772,37 +2767,37 @@ void __fastcall TSessionData::AddAssemblyProperty(
   AddAssemblyPropertyRaw(Result, Language, Name, PropertyValue);
 }
 //---------------------------------------------------------------------
-UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
-  TAssemblyLanguage Language)
+void __fastcall TSessionData::GenerateAssemblyCode(
+  TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent)
 {
   std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
   std::unique_ptr<TSessionData> SessionData(Clone());
 
-  UnicodeString Result;
+  UnicodeString SessionOptionsCommentLine =
+    AssemblyCommentLine(Language, LoadStr(CODE_SESSION_OPTIONS));
 
-  UnicodeString SessionOptionsPreamble;
   switch (Language)
   {
     case alCSharp:
-      SessionOptionsPreamble =
-        RtfCodeComment(L"// %s") + RtfPara +
+      Head +=
+        SessionOptionsCommentLine +
         RtfLibraryClass(L"SessionOptions") + RtfText(L" sessionOptions = ") + RtfKeyword(L"new") + RtfText(" ") + RtfLibraryClass(L"SessionOptions") + RtfPara +
         RtfText(L"{") + RtfPara;
       break;
 
     case alVBNET:
-      SessionOptionsPreamble =
-        RtfCodeComment(L"' %s") + RtfPara +
+      Head +=
+        SessionOptionsCommentLine +
         RtfKeyword(L"Dim") + RtfText(" mySessionOptions ") + RtfKeyword(L"As") + RtfText(L" ") + RtfKeyword(L"New") + RtfText(" ") + RtfLibraryClass(L"SessionOptions") + RtfPara +
         RtfKeyword(L"With") + RtfText(" mySessionOptions") + RtfPara;
       break;
 
     case alPowerShell:
-      SessionOptionsPreamble =
-        RtfCodeComment(FORMAT(L"# %s", (LoadStr(CODE_PS_ADD_TYPE)))) + RtfPara +
+      Head +=
+        AssemblyCommentLine(Language, LoadStr(CODE_PS_ADD_TYPE)) +
         RtfKeyword(L"Add-Type") + RtfText(" -Path ") + AssemblyString(Language, "WinSCPnet.dll") + RtfPara +
         RtfPara +
-        RtfCodeComment(L"# %s") + RtfPara +
+        SessionOptionsCommentLine +
         RtfText(L"$sessionOptions = ") + RtfKeyword(L"New-Object") + RtfText(" WinSCP.") + RtfLibraryClass(L"SessionOptions") + RtfPara;
       break;
 
@@ -2811,8 +2806,6 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
       break;
   }
 
-  Result = FORMAT(SessionOptionsPreamble, (LoadStr(CODE_SESSION_OPTIONS)));
-
   UnicodeString ProtocolMember;
   switch (SessionData->FSProtocol)
   {
@@ -2841,7 +2834,7 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
   bool AUsesSsh = SessionData->UsesSsh;
 
   // Protocol is set unconditionally, we want even the default SFTP
-  AddAssemblyProperty(Result, Language, L"Protocol", L"Protocol", ProtocolMember);
+  AddAssemblyProperty(Head, Language, L"Protocol", L"Protocol", ProtocolMember);
   // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
   if (SessionData->FSProtocol != fsSFTPonly)
   {
@@ -2849,22 +2842,22 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
   }
   if (SessionData->HostName != FactoryDefaults->HostName)
   {
-    AddAssemblyProperty(Result, Language, L"HostName", HostName);
+    AddAssemblyProperty(Head, Language, L"HostName", HostName);
     SessionData->HostName = FactoryDefaults->HostName;
   }
   if (SessionData->PortNumber != FactoryDefaults->PortNumber)
   {
-    AddAssemblyProperty(Result, Language, L"PortNumber", PortNumber);
+    AddAssemblyProperty(Head, Language, L"PortNumber", PortNumber);
     SessionData->PortNumber = FactoryDefaults->PortNumber;
   }
   if (SessionData->UserName != FactoryDefaults->UserName)
   {
-    AddAssemblyProperty(Result, Language, L"UserName", UserName);
+    AddAssemblyProperty(Head, Language, L"UserName", UserName);
     SessionData->UserName = FactoryDefaults->UserName;
   }
   if (SessionData->Password != FactoryDefaults->Password)
   {
-    AddAssemblyProperty(Result, Language, L"Password", Password);
+    AddAssemblyProperty(Head, Language, L"Password", Password);
     SessionData->Password = FactoryDefaults->Password;
   }
 
@@ -2897,12 +2890,12 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
               DebugFail();
               break;
           }
-          AddAssemblyProperty(Result, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
+          AddAssemblyProperty(Head, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
         }
         break;
 
       case fsWebDAV:
-        AddAssemblyProperty(Result, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
+        AddAssemblyProperty(Head, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
         break;
 
       default:
@@ -2915,39 +2908,39 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
   if (SessionData->HostKey != FactoryDefaults->HostKey)
   {
     UnicodeString PropertyName = AUsesSsh ? L"SshHostKeyFingerprint" : L"TlsHostCertificateFingerprint";
-    AddAssemblyProperty(Result, Language, PropertyName, SessionData->HostKey);
+    AddAssemblyProperty(Head, Language, PropertyName, SessionData->HostKey);
     SessionData->HostKey = FactoryDefaults->HostKey;
   }
   if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
   {
-    AddAssemblyProperty(Result, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
+    AddAssemblyProperty(Head, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
     SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
   }
   if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
   {
-    AddAssemblyProperty(Result, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
+    AddAssemblyProperty(Head, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
     SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
   }
   if (SessionData->Passphrase != FactoryDefaults->Passphrase)
   {
-    AddAssemblyProperty(Result, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
+    AddAssemblyProperty(Head, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
     SessionData->Passphrase = FactoryDefaults->Passphrase;
   }
   if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
   {
-    AddAssemblyProperty(Result, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
+    AddAssemblyProperty(Head, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
     SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
   }
   if (SessionData->Timeout != FactoryDefaults->Timeout)
   {
-    AddAssemblyProperty(Result, Language, L"TimeoutInMilliseconds", SessionData->Timeout);
+    AddAssemblyProperty(Head, Language, L"TimeoutInMilliseconds", SessionData->Timeout);
     SessionData->Timeout = FactoryDefaults->Timeout;
   }
 
   switch (Language)
   {
     case alCSharp:
-      Result += RtfText(L"};") + RtfPara;
+      Head += RtfText(L"};") + RtfPara;
       break;
 
     case alVBNET:
@@ -2960,7 +2953,7 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
 
   if (RawSettings->Count > 0)
   {
-    Result += RtfPara;
+    Head += RtfPara;
 
     for (int Index = 0; Index < RawSettings->Count; Index++)
     {
@@ -2981,49 +2974,49 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
           SettingsCode = RtfText(L"$sessionOptions.") + RtfLibraryMethod(L"SessionOptions", L"AddRawSettings") + RtfText(L"(%s, %s)") + RtfPara;
           break;
       }
-      Result += FORMAT(SettingsCode, (AssemblyString(Language, Name), AssemblyString(Language, Value)));
+      Head += FORMAT(SettingsCode, (AssemblyString(Language, Name), AssemblyString(Language, Value)));
     }
   }
 
-  UnicodeString SessionCode;
+  UnicodeString CodeCommentLine = L"    " + AssemblyCommentLine(Language, LoadStr(CODE_CONNECT));
 
   switch (Language)
   {
     case alCSharp:
-      SessionCode =
+      Head +=
         RtfPara +
         RtfKeyword(L"using") + RtfText(" (") + RtfLibraryClass(L"Session") + RtfText(L" session = ") + RtfKeyword(L"new") + RtfText(" ") + RtfLibraryClass(L"Session") + RtfText(L"())") + RtfPara +
         RtfText(L"{") + RtfPara +
-        RtfCodeComment(L"    // %s") + RtfPara +
-        RtfText(L"    session.") + RtfLibraryMethod(L"Session", L"Open") + RtfText(L"(sessionOptions);") + RtfPara +
-        RtfPara +
-        RtfCodeComment(L"    // %s") + RtfPara +
+        CodeCommentLine +
+        RtfText(L"    session.") + RtfLibraryMethod(L"Session", L"Open") + RtfText(L"(sessionOptions);") + RtfPara;
+
+      Tail =
         RtfText(L"}") + RtfPara;
       break;
 
     case alVBNET:
-      SessionCode =
+      Head +=
         RtfKeyword(L"End With") + RtfPara +
         RtfPara +
         RtfKeyword(L"Using") + RtfText(" mySession As ") + RtfLibraryClass(L"Session") + RtfText(L" = ") + RtfKeyword(L"New") + RtfText(" ") + RtfLibraryClass(L"Session") + RtfPara +
-        RtfCodeComment(L"    ' %s") + RtfPara +
-        RtfText(L"    mySession.") + RtfLibraryMethod(L"Session", L"Open") + RtfText(L"(mySessionOptions)") + RtfPara +
-        RtfPara +
-        RtfCodeComment(L"    ' %s") + RtfPara +
+        CodeCommentLine +
+        RtfText(L"    mySession.") + RtfLibraryMethod(L"Session", L"Open") + RtfText(L"(mySessionOptions)") + RtfPara;
+
+      Tail =
         RtfKeyword(L"End Using");
       break;
 
     case alPowerShell:
-      SessionCode =
+      Head +=
         RtfPara +
         RtfText(L"$session = ") + RtfKeyword(L"New-Object") + RtfText(" WinSCP.") + RtfLibraryClass(L"Session") + RtfPara +
         RtfPara +
         RtfKeyword(L"try") + RtfPara +
         RtfText(L"{") + RtfPara +
-        RtfCodeComment(L"    # %s") + RtfPara +
-        RtfText(L"    $session.") + RtfLibraryMethod(L"Session", L"Open") + RtfText(L"($sessionOptions)") + RtfPara +
-        RtfPara +
-        RtfCodeComment(L"    # %s") + RtfPara +
+        CodeCommentLine +
+        RtfText(L"    $session.") + RtfLibraryMethod(L"Session", L"Open") + RtfText(L"($sessionOptions)") + RtfPara;
+
+      Tail =
         RtfText(L"}") + RtfPara +
         RtfKeyword(L"finally") + RtfPara +
         RtfText(L"{") + RtfPara +
@@ -3032,9 +3025,9 @@ UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
       break;
   }
 
-  Result += FORMAT(SessionCode, (LoadStr(CODE_CONNECT), LoadStr(CODE_YOUR_CODE)));
+  Head += RtfPara;
 
-  return Result;
+  Indent = 4; // the same for all languages so far
 }
 //---------------------------------------------------------------------
 void __fastcall TSessionData::SetTimeDifference(TDateTime value)

+ 1 - 1
source/core/SessionData.h

@@ -435,7 +435,7 @@ public:
   bool __fastcall IsInFolderOrWorkspace(UnicodeString Name);
   UnicodeString __fastcall GenerateSessionUrl(unsigned int Flags);
   UnicodeString __fastcall GenerateOpenCommandArgs();
-  UnicodeString __fastcall GenerateAssemblyCode(TAssemblyLanguage Language);
+  void __fastcall GenerateAssemblyCode(TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent);
   void __fastcall LookupLastFingerprint();
   bool __fastcall IsSecure();
   static void __fastcall ValidatePath(const UnicodeString Path);

+ 15 - 1
source/forms/GenerateUrl.cpp

@@ -536,7 +536,21 @@ void __fastcall TGenerateUrlDialog::UpdateControls()
     }
     else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
     {
-      Result = FData->GenerateAssemblyCode(static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex));
+      TAssemblyLanguage Language = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
+
+      UnicodeString Head;
+      UnicodeString Tail;
+      int Indent;
+
+      FData->GenerateAssemblyCode(Language, Head, Tail, Indent);
+
+      UnicodeString Indentation = UnicodeString::StringOfChar(L' ', Indent);
+
+      Result =
+        Head +
+        Indentation + AssemblyCommentLine(Language, LoadStr(GENERATE_URL_YOUR_CODE)) +
+        Tail;
+
       WordWrap = false;
       FixedWidth = true;
     }

+ 0 - 1
source/resource/TextsCore.h

@@ -450,7 +450,6 @@
 #define SCRIPT_SITE_WARNING     549
 #define CODE_SESSION_OPTIONS    550
 #define CODE_CONNECT            551
-#define CODE_YOUR_CODE          552
 #define CODE_PS_ADD_TYPE        553
 #define COPY_INFO_PRESERVE_TIME_DIRS 554
 

+ 0 - 1
source/resource/TextsCore1.rc

@@ -420,7 +420,6 @@ BEGIN
   SCRIPT_SITE_WARNING, "In scripting you should not rely on saved sites, use this command instead:"
   CODE_SESSION_OPTIONS, "Setup session options"
   CODE_CONNECT, "Connect"
-  CODE_YOUR_CODE, "Your code"
   CODE_PS_ADD_TYPE, "Load WinSCP .NET assembly"
   COPY_INFO_PRESERVE_TIME_DIRS, "%s (including directories)"
 

+ 1 - 0
source/resource/TextsWin.h

@@ -566,6 +566,7 @@
 #define GENERATE_URL_COPY_PARAM_SCRIPT_REMAINING 1964
 #define GENERATE_URL_SCRIPT_DESC 1965
 #define GENERATE_URL_PATH_TO_SCRIPT 1966
+#define GENERATE_URL_YOUR_CODE  1967
 
 // 2xxx is reserved for TextsFileZilla.h
 

+ 1 - 0
source/resource/TextsWin1.rc

@@ -569,6 +569,7 @@ BEGIN
         GENERATE_URL_COPY_PARAM_SCRIPT_REMAINING, "Some configured transfer settings do not have a scripting equivalent. Use the /rawconfig command-line switch instead."
         GENERATE_URL_SCRIPT_DESC, "Execute the script using a command like:"
         GENERATE_URL_PATH_TO_SCRIPT, "C:\\path\\to\\script\\script.txt"
+        GENERATE_URL_YOUR_CODE, "Your code"
 
         WIN_VARIABLE_STRINGS, "WIN_VARIABLE"
         WINSCP_COPYRIGHT, "Copyright © 2000-2016 Martin Prikryl"

+ 1 - 1
source/windows/WinConfiguration.cpp

@@ -1,8 +1,8 @@
 //---------------------------------------------------------------------------
 #include <vcl.h>
 #pragma hdrstop
-#include "WinConfiguration.h"
 #include "Common.h"
+#include "WinConfiguration.h"
 #include "Exceptions.h"
 #include "Bookmarks.h"
 #include "Terminal.h"