aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Uninstaller/MainWindow.xaml.cs
blob: c3a0133ee9ac43d8c15ee44e0a20c11c4c1a5d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.Windows;

namespace MediaBrowser.Uninstaller.Execute
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        protected string Product = "Server";
        protected string RootSuffix = "-Server";

        public MainWindow()
        {

            var args = Environment.GetCommandLineArgs();
            var product = args.Length > 1 ? args[1] : null;
            if (product == null)
            {
                MessageBox.Show("Please use 'Programs and Features' to uninstall.");
                Close();
            }
            else
            {
                var callerId = args.Length > 2 ? args[2] : null;
                if (callerId != null)
                {
                    // Wait for our caller to exit
                    try
                    {
                        var process = Process.GetProcessById(Convert.ToInt32(callerId));
                        process.WaitForExit();
                    }
                    catch (ArgumentException)
                    {
                        // wasn't running
                    }
                }
                else
                {
                    // No caller - means we were called directly and need to move to temp file and execute there
                    //copy the real program to a temp location so we can delete everything here (including us)
                    var us = Assembly.GetExecutingAssembly().Location;
                    var tempExe = Path.Combine(Path.GetTempPath(), Path.GetFileName(us) ?? "Mediabrowser.Uninstaller.exe");
                    File.Copy(us,tempExe,true);
                    //get our pid to pass to the uninstaller so it can wait for us to exit
                    var pid = Process.GetCurrentProcess().Id;
                    //kick off the copy
                    Process.Start(tempExe, product + " " + pid);
                    //and shut down
                    Close();
                }

                InitializeComponent();


                switch (product)
                {
                    case "server":
                        Product = "Server";
                        RootSuffix = "-Server";
                        break;

                    case "mbt":
                        Product = "Theater";
                        RootSuffix = "-Theater";
                        break;

                    default:
                        MessageBox.Show("Please Use 'Programs and Features' to uninstall.");
                        Close();
                        break;

                }

                lblHeading.Content = this.Title = "Uninstall Media Browser " + Product;
                
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void cbxRemoveAll_Checked(object sender, RoutedEventArgs e)
        {
            if (cbxRemoveAll.IsChecked == true)
            {
                cbxRemoveCache.IsChecked = cbxRemoveConfig.IsChecked = cbxRemovePlugins.IsChecked = true;
            }

            cbxRemoveCache.IsEnabled = cbxRemoveConfig.IsEnabled = cbxRemovePlugins.IsEnabled = !cbxRemoveAll.IsChecked.Value;
        }

        private void btnUninstall_Click(object sender, RoutedEventArgs e)
        {
            // First remove our shortcuts
            lblHeading.Content = "Removing Shortcuts...";
            btnCancel.IsEnabled = btnUninstall.IsEnabled = false;
            grdOptions.Visibility = Visibility.Hidden;

            var startMenu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3");
            var linkName = "Media Browser " + Product + ".lnk";
            RemoveShortcut(Path.Combine(startMenu, linkName));
            RemoveShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),linkName));
            linkName = "Uninstall " + linkName;
            RemoveShortcut(Path.Combine(startMenu, linkName));
            if (Product == "Server")
            {
                RemoveShortcut(Path.Combine(startMenu, "MB Dashboard.lnk"));
                var procs = Process.GetProcessesByName("mediabrowser.serverapplication");
                var server = procs.Length > 0 ? procs[0] : null;
                if (server != null)
                {
                    using (var client = new WebClient())
                    {
                        lblHeading.Content = "Shutting Down Media Browser Server...";
                        try
                        {
                            client.UploadString("http://localhost:8096/mediabrowser/system/shutdown", "");
                            try
                            {
                                server.WaitForExit();
                            }
                            catch (ArgumentException)
                            {
                                // already gone
                            }
                        }
                        catch (WebException ex)
                        {
                            if (ex.Status != WebExceptionStatus.ConnectFailure && !ex.Message.StartsWith("Unable to connect", StringComparison.OrdinalIgnoreCase))
                            {
                                MessageBox.Show("Error shutting down server.  Please be sure it is not running before hitting OK.\n\n" + ex.Status + "\n\n" + ex.Message);
                            }
                        }
                    }
                }
            }
            else
            {
                // Installing MBT - shut it down if it is running
                var processes = Process.GetProcessesByName("mediabrowser.ui");
                if (processes.Length > 0)
                {
                    lblHeading.Content = "Shutting Down Media Browser Theater...";
                    try
                    {
                        processes[0].Kill();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Unable to shutdown Media Browser Theater.  Please ensure it is not running before hitting OK.\n\n" + ex.Message, "Error");
                    }
                }
            }
            // if the startmenu item is empty now - delete it too
            if (Directory.GetFiles(startMenu).Length == 0)
            {
                try
                {
                    Directory.Delete(startMenu);
                }
                catch (DirectoryNotFoundException)
                {
                }
                catch (Exception ex)
                {
                    {
                        MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
                    }
                }
            }

            var rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);

            lblHeading.Content = "Removing System Files...";
            if (cbxRemoveAll.IsChecked == true)
            {
                // Just remove our whole directory
                RemovePath(rootPath);
            }
            else
            {
                // First remove the system
                RemovePath(Path.Combine(rootPath, "System"));
                RemovePath(Path.Combine(rootPath, "MediaTools"));

                // And then the others specified
                if (cbxRemoveCache.IsChecked == true)
                {
                    lblHeading.Content = "Removing Cache and Data Files...";
                    RemovePath(Path.Combine(rootPath, "cache"));
                    RemovePath(Path.Combine(rootPath, "data"));
                }
                if (cbxRemoveConfig.IsChecked == true)
                {
                    lblHeading.Content = "Removing Config Files...";
                    RemovePath(Path.Combine(rootPath, "config"));
                    RemovePath(Path.Combine(rootPath, "logs"));
                }
                if (cbxRemovePlugins.IsChecked == true)
                {
                    lblHeading.Content = "Removing Plugin Files...";
                    RemovePath(Path.Combine(rootPath, "plugins"));
                }
            }

            // Remove reference to us
            RemoveUninstall();

            // Remove pismo
            try
            {
                UnInstallPismo();
            }
            catch
            {
                // No biggie - maybe they uninstalled it themselves
            }

            // and done
            lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
            btnUninstall.Visibility = Visibility.Hidden;
            btnFinished.Visibility = Visibility.Visible;
        }

        private void UnInstallPismo()
        {
            // Kick off the Pismo uninstaller and wait for it to end
            var pismo = new Process();
            pismo.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            pismo.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "pfm.exe");
            pismo.StartInfo.Arguments = "uninstall pfm-license-mediabrowser.txt";
            pismo.Start();
            pismo.WaitForExit();
        }

        private void RemoveUninstall()
        {

            using (var parent = Registry.CurrentUser.OpenSubKey(
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true))
            {
                if (parent == null)
                {
                    MessageBox.Show("Uninstall registry key not found.");
                    return;
                }
                try
                {
                    const string guidText = "{4E76DB4E-1BB9-4A7B-860C-7940779CF7A0}";
                    parent.DeleteSubKey(guidText,false);

                }
                catch (Exception ex)
                {
                    throw new Exception(
                        "An error occurred removing uninstall information from the registry.",
                        ex);
                }
            }
        }

        private static
            void RemoveShortcut(string path)
        {
            try
            {
                File.Delete(path);
            }
            catch (FileNotFoundException)
            {
            } // we're trying to get rid of it anyway
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
            }

        }

        private static void RemovePath(string path)
        {
            try
            {
                Directory.Delete(path, true);
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error attempting to remove progam folder {0}\n\n {1}", path, ex.Message), "Error");
            }
            
        }

        private void BtnFinished_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}