瀏覽代碼

Added Protocol property to AutoDiscovery server; Improved email mobile link sending.

Kenric Nugteren 1 年之前
父節點
當前提交
91000076d6

+ 5 - 0
prs.classes/Server/Properties/AutoDiscoveryServerProperties.cs

@@ -1,5 +1,6 @@
 using System;
 using Comal.Classes;
+using InABox.Clients;
 using InABox.Core;
 
 namespace PRSServer
@@ -30,6 +31,10 @@ namespace PRSServer
         [TextBoxEditor]
         [EditorSequence(2)]
         public String ServerURLs { get; set; }
+
+        [EnumLookupEditor(typeof(SerializerProtocol))]
+        [EditorSequence(3)]
+        public SerializerProtocol Protocol { get; set; } = SerializerProtocol.RPC;
         
         [FolderEditor(Environment.SpecialFolder.CommonDocuments)]
         [EditorSequence(9)]

+ 5 - 4
prs.desktop/App.xaml.cs

@@ -17,6 +17,7 @@ using Comal.Classes;
 using InABox.Configuration;
 using InABox.Core;
 using InABox.Logging;
+using InABox.Wpf;
 using InABox.WPF;
 using InABox.WPF.Themes;
 using NDesk.Options;
@@ -63,8 +64,8 @@ namespace PRSDesktop
 
         private void AutoDiscover(Dictionary<string, DatabaseSettings> allsettings)
         {
-            var confirm = MessageBox.Show("Try to configure Server Connection Automatically?", "Auto Discover", MessageBoxButton.YesNoCancel);
-            if (confirm == MessageBoxResult.Yes)
+            var confirm = MessageWindow.ShowYesNoCancel("Try to configure Server Connection Automatically?", "Auto Discover");
+            if (confirm == MessageWindowResult.Yes)
                 try
                 {
                     using (new WaitCursor())
@@ -108,12 +109,12 @@ namespace PRSDesktop
                         };
                         new LocalConfiguration<AutoUpdateSettings>().Save(AutoUpdateSettings);
 
-                        MessageBox.Show($"Server found at {String.Join(";",autodiscover.URLs)}");
+                        MessageWindow.ShowMessage($"Server found at {String.Join(";",autodiscover.URLs)}", "Success");
                     }
                 }
                 catch
                 {
-                    MessageBox.Show("No Server Found");
+                    MessageWindow.ShowMessage("No Server Found", "Not found", image: MessageWindow.WarningImage);
                 }
         }
 

+ 20 - 8
prs.desktop/Configuration/DataBaseConfiguration.xaml.cs

@@ -506,8 +506,7 @@ namespace PRSDesktop
         //    }
         //}
 
-
-        private void AutoDiscover_Click(object sender, RoutedEventArgs e)
+        public static AutoDiscoverySettings? AutoDiscoverServer()
         {
             Progress.Show("Looking for available Servers...");
             try
@@ -527,6 +526,24 @@ namespace PRSDesktop
                 var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
                 var autodiscover = Serialization.Deserialize<AutoDiscoverySettings>(ServerResponse);
 
+                Client.Close();
+                Progress.Close();
+                return autodiscover;
+            }
+            catch (Exception err)
+            {
+                Logger.Send(LogType.Error, "", $"No server found: {CoreUtils.FormatException(err)}");
+                return null;
+            }
+        }
+
+        private void AutoDiscover_Click(object sender, RoutedEventArgs e)
+        {
+            Progress.Show("Looking for available Servers...");
+            var autodiscover = AutoDiscoverServer();
+            Progress.Close();
+            if(autodiscover is not null)
+            {
                 ProfileName.Text = autodiscover.Name;
                 IsActiveCheck.IsChecked = true;
                 SplashSelect.Tag = autodiscover.Logo;
@@ -546,16 +563,11 @@ namespace PRSDesktop
                 LibraryLocation.Text = autodiscover.LibraryLocation;
                 GoogleAPIKey.Text = autodiscover.GoogleAPIKey;
 
-                Client.Close();
-                Progress.Close();
-
                 MessageBox.Show(String.Format("Server found at {0}", String.Join(";",autodiscover.URLs)));
                 UnloadSettings();
             }
-            catch (Exception err)
+            else
             {
-                Logger.Send(LogType.Error, "", $"No server found: {CoreUtils.FormatException(err)}");
-                Progress.Close();
                 MessageBox.Show("No Server Found!");
             }
         }

+ 65 - 39
prs.desktop/Panels/Users/UserGrid.cs

@@ -47,6 +47,28 @@ namespace PRSDesktop
                 DynamicGridOption.SelectColumns);
         }
 
+        private class MobileLinkData : BaseObject
+        {
+            [TextBoxEditor]
+            [EditorSequence(1)]
+            public string ServerURLS { get; set; } = "";
+
+            [TextBoxEditor]
+            [EditorSequence(2)]
+            public string From { get; set; } = "";
+
+            [TextBoxEditor]
+            [EditorSequence(3)]
+            public string To { get; set; } = "";
+
+            [IntegerEditor(ToolTip = "Enter link expiry time (mins)")]
+            [EditorSequence(4)]
+            public int ExpiryTime { get; set; } = 10;
+        }
+
+        private static readonly string IOSLink = @"prsmobile://open/";
+        private static readonly string AndroidLink = @"http://www.prsmobile.com/open/";
+
         private bool SendEmail(CoreRow? row)
         {
             if (row is null) 
@@ -54,55 +76,59 @@ namespace PRSDesktop
 
             User user = row.ToObject<User>();
 
-            var menu = new ContextMenu();
-            menu.AddItem("PRS Site App", null, () =>
+            var data = new MobileLinkData
             {
-                CreateLink(user, @"prssite://open/", @"http://www.prssite.com/open/", CreateURLs(new string[] { "remote.com-al.com.au:8050" }));
-            });
-
-            menu.AddItem("PRS Timebench", null, () =>
+                ServerURLS = string.Join(';', App.DatabaseSettings.URLs),
+                From = EmailUtils.GetAddressFromUserOrNull() ?? "",
+                To = user.EmailAddress,
+                ExpiryTime = 10
+            };
+            if (DynamicGridUtils.Edit(data, customiseGrid: (grid) =>
             {
-                CreateLink(user, @"prsmobile://open/", @"http://www.prsmobile.com/open/", CreateURLs(App.DatabaseSettings.URLs));
-            });
-            menu.IsOpen = true;    
-            return true;
+                grid.OnLoadEditorButtons += (item, buttons) =>
+                {
+                    buttons.Add("Scan for URL", null, null, (s, o) =>
+                    {
+                        Progress.Show("Looking for available Servers...");
+                        var settings = DataBaseConfiguration.AutoDiscoverServer();
+                        Progress.Close();
+                        if(settings is not null && settings.Protocol == SerializerProtocol.RPC)
+                        {
+                            data.ServerURLS = string.Join(';', settings.URLs);
+                            MessageWindow.ShowMessage("Server found.", "Success");
+                        }
+                        else
+                        {
+                            MessageWindow.ShowMessage("No RPC server found.", "Not found");
+                        }
+                    });
+                };
+            }))
+            {
+                CreateLink(user, data);
+            }
+
+            return false;
         }
 
-        private static void CreateLink(User user, string ioslink, string androidlink, string URLs)
+        private static void CreateLink(User user, MobileLinkData data)
         {
-            if (string.IsNullOrWhiteSpace(ioslink))
-                return;
+            var URLs = CreateURLs(data.ServerURLS.Split(';'));
+            var toEncrypt = URLs + "," + user.UserID + "," + user.Password + "," + DateTime.Now.AddMinutes(data.ExpiryTime);
+            var encrypted = Encryption.Encrypt(toEncrypt, "logindetailslink", true);
 
-            var edt = new NumberEdit("Enter link expiry time in minutes", 10, 300, 10);
-            bool result = (bool)edt.ShowDialog();
-            if (!result)
-                return;
+            var emailcontent = $"Please ensure PRS Mobile is closed, then choose a link below:\n\n" +
+                $"For Apple devices, click this link: {IOSLink}{encrypted}\n\n" +
+                $"For Android devices (Samsung, Google, Xiaomi, Oppo, Vivo, Huawei, Motorola etc), click this link: {AndroidLink}{encrypted}\n\n" +
+                $"Please restart the app after loading from the link.\n\nThese links will expire after {data.ExpiryTime} minutes.";
 
-            var expiry = edt.Value;
-
-            string toEncrypt = URLs + "," + user.UserID + "," + user.Password + "," + DateTime.Now.AddMinutes(expiry);
-            string encrypted = Encryption.Encrypt(toEncrypt, "logindetailslink", true);
-
-            ioslink += encrypted;
-            androidlink += encrypted;
-
-            string emailcontent = "Please ensure PRS Mobile is closed, then choose a link below:" + Environment.NewLine + Environment.NewLine +
-                "For Apple devices, click this link: " + ioslink + Environment.NewLine + Environment.NewLine
-                + "For Android devices (Samsung, Google, Xiaomi, Oppo, Vivo, Huawei, Motorola etc), click this link: " + androidlink + Environment.NewLine + Environment.NewLine +
-                "Please restart the app after loading from the link." + Environment.NewLine + Environment.NewLine +
-                "These links will expire after " + expiry + " minutes.";
-
-            EmailUtils.CreateEMLFile(user.EmailAddress, "PRS Mobile Configuration Links", emailcontent);
+            var message = EmailUtils.CreateMessage(from: data.From, subject: "PRS Mobile Configuration Links", body: emailcontent, to: data.To);
+            EmailUtils.OpenEmail(message);
         }
 
-        private string CreateURLs(string[] urls)
+        private static string CreateURLs(string[] urls)
         {
-            string URLs = "";
-            foreach (var url in urls)
-            { 
-                URLs = URLs + url + ",";
-            }
-            return URLs + "ENDURLS";
+            return string.Join("", urls.Select(x => $"{x},")) + "ENDURLS";
         }
 
         private BitmapImage? EmailImage(CoreRow? arg)

+ 1 - 1
prs.server/Engines/AutoDiscovery/AutoDiscoveryEngine.cs

@@ -25,7 +25,7 @@ internal class AutoDiscoveryEngine : Engine<AutoDiscoveryServerProperties>
         }
         settings.URLs = urls.Split(';');
 
-        settings.Protocol = SerializerProtocol.Rest;
+        settings.Protocol = Properties.Protocol;
         settings.LibraryLocation = Properties.LibraryLocation;
         settings.GoogleAPIKey = Properties.GoogleAPIKey;
         var responseData = Encoding.ASCII.GetBytes(Serialization.Serialize(settings));