1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2020 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.Diagnostics;
23 using System.IO;
24 using System.Net;
25 using System.Threading;
26 using System.Xml;
27 
28 namespace GKCore
29 {
30     /// <summary>
31     ///
32     /// </summary>
33     public static class UpdateMan
34     {
35         private const string UpdateURL = "https://sourceforge.net/projects/gedkeeper/files/gk_version.xml";
36 
GetLastVersion(out string url)37         public static Version GetLastVersion(out string url)
38         {
39             Version newVersion = null;
40             url = "";
41 
42             XmlTextReader reader = null;
43             try {
44                 GKUtils.InitSecurityProtocol();
45 
46                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UpdateURL);
47                 webRequest.ContentType = "text/xml; encoding='utf-8'";
48                 webRequest.KeepAlive = false;
49                 webRequest.Method = "GET";
50 
51                 using (WebResponse webResponse = webRequest.GetResponse()) {
52                     using (Stream stream = webResponse.GetResponseStream()) {
53                         reader = new XmlTextReader(stream);
54                         reader.MoveToContent();
55 
56                         string nodeName = "";
57                         if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "gedkeeper")) {
58                             while (reader.Read()) {
59                                 if (reader.NodeType == XmlNodeType.Element)
60                                     nodeName = reader.Name;
61                                 else {
62                                     if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue)) {
63                                         switch (nodeName) {
64                                             case "version":
65                                                 newVersion = new Version(reader.Value);
66                                                 break;
67 
68                                             case "url":
69                                                 url = reader.Value;
70                                                 break;
71                                         }
72                                     }
73                                 }
74                             }
75                         }
76                     }
77                 }
78             } catch (Exception ex) {
79                 Logger.WriteError("UpdateMan.GetLastVersion()", ex);
80             } finally {
81                 if (reader != null)
82                     reader.Close();
83             }
84 
85             return newVersion;
86         }
87 
WorkerMethod()88         private static void WorkerMethod()
89         {
90             try {
91                 Version curVersion = AppHost.GetAppVersion();
92                 if (curVersion == null) return;
93 
94                 string url;
95                 Version newVersion = GetLastVersion(out url);
96                 if (newVersion == null) return;
97 
98                 if (curVersion.CompareTo(newVersion) < 0) {
99                     #if !CI_MODE
100                     string question = LangMan.LS(LSID.LSID_UpdateToLatestVersion, curVersion, newVersion);
101                     if (AppHost.StdDialogs.ShowQuestionYN(question)) {
102                         Process.Start(url);
103                     }
104                     #endif
105                 }
106             } catch (Exception ex) {
107                 Logger.WriteError("UpdateMan.WorkerMethod()", ex);
108             }
109         }
110 
CheckUpdate()111         public static void CheckUpdate()
112         {
113             try {
114                 #if MONO
115                 DesktopType desktopType = SysUtils.GetDesktopType();
116                 if (desktopType == DesktopType.Unity) {
117                     // In Ubuntu 1604 LTS (Unity desktop), this method leads to a
118                     // complete crash of the program at the level of X11,
119                     // but in the same version of Ubuntu and Xfce, everything is fine
120                     Logger.WriteInfo("UpdateMan.CheckUpdate(): is not supported for Unity");
121                     return;
122                 }
123                 #endif
124 
125                 Thread worker = new Thread(WorkerMethod);
126                 worker.SetApartmentState(ApartmentState.STA);
127                 worker.IsBackground = true;
128                 worker.Start();
129             } catch (Exception ex) {
130                 Logger.WriteError("UpdateMan.CheckUpdate()", ex);
131             }
132         }
133     }
134 }
135