~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.Gui/Mono.Addins.Gui/InstallDialog.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// InstallDialog.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using Mono.Addins.Setup;
 
28
using Mono.Addins.Description;
 
29
using System.Text;
 
30
using Mono.Unix;
 
31
using System.Threading;
 
32
using System.Linq;
 
33
using System.Collections.Generic;
 
34
 
 
35
namespace Mono.Addins.Gui
 
36
{
 
37
        internal partial class InstallDialog : Gtk.Dialog
 
38
        {
 
39
                string[] filesToInstall;
 
40
                AddinRepositoryEntry[] addinsToInstall;
 
41
                PackageCollection packagesToInstall;
 
42
                SetupService service;
 
43
                Gtk.ResponseType response = Gtk.ResponseType.None;
 
44
                IEnumerable<string> uninstallIds;
 
45
                InstallMonitor installMonitor;
 
46
                bool installing;
 
47
                const int MaxHeight = 350;
 
48
                
 
49
                public InstallDialog (Gtk.Window parent, SetupService service)
 
50
                {
 
51
                        this.Build ();
 
52
                        this.service = service;
 
53
                        TransientFor = parent;
 
54
                        WindowPosition = Gtk.WindowPosition.CenterOnParent;
 
55
                        Services.PlaceDialog (this, parent);
 
56
                        boxProgress.Visible = false;
 
57
                        Resizable = false;
 
58
                }
 
59
                
 
60
                public void InitForInstall (AddinRepositoryEntry[] addinsToInstall)
 
61
                {
 
62
                        this.addinsToInstall = addinsToInstall;
 
63
                        FillSummaryPage ();
 
64
                        Services.PlaceDialog (this, TransientFor);
 
65
                }
 
66
                
 
67
                public void InitForInstall (string[] filesToInstall)
 
68
                {
 
69
                        this.filesToInstall = filesToInstall;
 
70
                        FillSummaryPage ();
 
71
                        Services.PlaceDialog (this, TransientFor);
 
72
                }
 
73
                
 
74
                public void InitForUninstall (Addin[] info)
 
75
                {
 
76
                        this.uninstallIds = info.Select (a => a.Id);
 
77
                        buttonOk.Label = Catalog.GetString ("Uninstall");
 
78
                        
 
79
                        HashSet<Addin> sinfos = new HashSet<Addin> ();
 
80
                        
 
81
                        StringBuilder sb = new StringBuilder ();
 
82
                        sb.Append ("<b>").Append (Catalog.GetString ("The following packages will be uninstalled:")).Append ("</b>\n\n");
 
83
                        foreach (var a in info) {
 
84
                                sb.Append (a.Name + "\n\n");
 
85
                                sinfos.UnionWith (service.GetDependentAddins (a.Id, true));
 
86
                        }
 
87
                        
 
88
                        if (sinfos.Count > 0) {
 
89
                                sb.Append ("<b>").Append (Catalog.GetString ("There are other add-ins that depend on the previous ones which will also be uninstalled:")).Append ("</b>\n\n");
 
90
                                foreach (Addin si in sinfos)
 
91
                                        sb.Append (si.Description.Name + "\n");
 
92
                        }
 
93
                        
 
94
                        ShowMessage (sb.ToString ());
 
95
                        Services.PlaceDialog (this, TransientFor);
 
96
                }
 
97
                
 
98
                void FillSummaryPage ()
 
99
                {
 
100
                        PackageCollection packs = new PackageCollection ();
 
101
                        
 
102
                        if (filesToInstall != null) {
 
103
                                foreach (string file in filesToInstall) {
 
104
                                        packs.Add (Package.FromFile (file));
 
105
                                }
 
106
                        }
 
107
                        else {
 
108
                                foreach (AddinRepositoryEntry arep in addinsToInstall) {
 
109
                                        packs.Add (Package.FromRepository (arep));
 
110
                                }
 
111
                        }
 
112
                        
 
113
                        packagesToInstall = new PackageCollection (packs);
 
114
                        
 
115
                        PackageCollection toUninstall;
 
116
                        DependencyCollection unresolved;
 
117
                        bool res;
 
118
                        
 
119
                        InstallMonitor m = new InstallMonitor ();
 
120
                        res = service.ResolveDependencies (m, packs, out toUninstall, out unresolved);
 
121
                        
 
122
                        StringBuilder sb = new StringBuilder ();
 
123
                        if (!res) {
 
124
                                sb.Append ("<b><span foreground=\"red\">").Append (Catalog.GetString ("The selected add-ins can't be installed because there are dependency conflicts.")).Append ("</span></b>\n");
 
125
                                foreach (string s in m.Errors) {
 
126
                                        sb.Append ("<b><span foreground=\"red\">" + s + "</span></b>\n");
 
127
                                }
 
128
                                sb.Append ("\n");
 
129
                        }
 
130
                        
 
131
                        if (m.Warnings.Count != 0) {
 
132
                                foreach (string w in m.Warnings) {
 
133
                                        sb.Append ("<b><span foreground=\"red\">" + w + "</span></b>\n");
 
134
                                }
 
135
                                sb.Append ("\n");
 
136
                        }
 
137
                        
 
138
                        sb.Append ("<b>").Append (Catalog.GetString ("The following packages will be installed:")).Append ("</b>\n\n");
 
139
                        foreach (Package p in packs) {
 
140
                                sb.Append (p.Name);
 
141
                                if (!p.SharedInstall)
 
142
                                        sb.Append (Catalog.GetString (" (in user directory)"));
 
143
                                sb.Append ("\n");
 
144
                        }
 
145
                        sb.Append ("\n");
 
146
                        
 
147
                        if (toUninstall.Count > 0) {
 
148
                                sb.Append ("<b>").Append (Catalog.GetString ("The following packages need to be uninstalled:")).Append ("</b>\n\n");
 
149
                                foreach (Package p in toUninstall) {
 
150
                                        sb.Append (p.Name + "\n");
 
151
                                }
 
152
                                sb.Append ("\n");
 
153
                        }
 
154
                        
 
155
                        if (unresolved.Count > 0) {
 
156
                                sb.Append ("<b>").Append (Catalog.GetString ("The following dependencies could not be resolved:")).Append ("</b>\n\n");
 
157
                                foreach (Dependency p in unresolved) {
 
158
                                        sb.Append (p.Name + "\n");
 
159
                                }
 
160
                                sb.Append ("\n");
 
161
                        }
 
162
                        buttonOk.Sensitive = res;
 
163
                        ShowMessage (sb.ToString ());
 
164
                }
 
165
                
 
166
                void ShowMessage (string txt)
 
167
                {
 
168
                        labelInfo.Markup = txt.TrimEnd ('\n','\t',' ');
 
169
                        if (labelInfo.SizeRequest ().Height > MaxHeight) {
 
170
                                scrolledwindow1.VscrollbarPolicy = Gtk.PolicyType.Automatic;
 
171
                                scrolledwindow1.HeightRequest = MaxHeight;
 
172
                        }
 
173
                        else {
 
174
                                scrolledwindow1.HeightRequest = labelInfo.SizeRequest ().Height;
 
175
                        }
 
176
                }
 
177
                
 
178
                protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
 
179
                {
 
180
                        if (response != Gtk.ResponseType.None) {
 
181
                                Respond (response);
 
182
                                return;
 
183
                        }
 
184
                        Install ();
 
185
                }
 
186
                
 
187
                protected virtual void OnButtonCancelClicked (object sender, System.EventArgs e)
 
188
                {
 
189
                        if (installing) {
 
190
                                if (Services.AskQuestion (Catalog.GetString ("Are you sure you want to cancel the installation?")))
 
191
                                        installMonitor.Cancel ();
 
192
                        } else
 
193
                                Respond (Gtk.ResponseType.Cancel);
 
194
                }
 
195
                
 
196
                void Install ()
 
197
                {
 
198
                        insSeparator.Visible = true;
 
199
                        boxProgress.Visible = true;
 
200
                        buttonOk.Sensitive = false;
 
201
                        
 
202
                        string txt;
 
203
                        string errmessage;
 
204
                        string warnmessage;
 
205
                        
 
206
                        ThreadStart oper;
 
207
                                
 
208
                        if (uninstallIds == null) {
 
209
                                installMonitor = new InstallMonitor (globalProgressLabel, mainProgressBar, Catalog.GetString ("Installing Add-ins"));
 
210
                                oper = new ThreadStart (RunInstall);
 
211
                                errmessage = Catalog.GetString ("The installation failed!");
 
212
                                warnmessage = Catalog.GetString ("The installation has completed with warnings.");
 
213
                        } else {
 
214
                                installMonitor = new InstallMonitor (globalProgressLabel, mainProgressBar, Catalog.GetString ("Uninstalling Add-ins"));
 
215
                                oper = new ThreadStart (RunUninstall);
 
216
                                errmessage = Catalog.GetString ("The uninstallation failed!");
 
217
                                warnmessage = Catalog.GetString ("The uninstallation has completed with warnings.");
 
218
                        }
 
219
                        
 
220
                        installing = true;
 
221
                        oper ();
 
222
                        installing = false;
 
223
                        
 
224
                        buttonCancel.Visible = false;
 
225
                        buttonOk.Label = Gtk.Stock.Close;
 
226
                        buttonOk.UseStock = true;
 
227
                        
 
228
                        if (installMonitor.Success && installMonitor.Warnings.Count == 0) {
 
229
                                Respond (Gtk.ResponseType.Ok);
 
230
                                return;
 
231
                        } else if (installMonitor.Success) {
 
232
                                txt = "<b>" + warnmessage + "</b>\n\n";
 
233
                                foreach (string s in installMonitor.Warnings)
 
234
                                        txt += GLib.Markup.EscapeText (s) + "\n";
 
235
                                response = Gtk.ResponseType.Ok;
 
236
                                buttonOk.Sensitive = true;
 
237
                        } else {
 
238
                                buttonCancel.Label = Gtk.Stock.Close;
 
239
                                buttonCancel.UseStock = true;
 
240
                                txt = "<span foreground=\"red\"><b>" + errmessage + "</b></span>\n\n";
 
241
                                foreach (string s in installMonitor.Errors)
 
242
                                        txt += GLib.Markup.EscapeText (s) + "\n";
 
243
                                response = Gtk.ResponseType.Cancel;
 
244
                                buttonOk.Sensitive = true;
 
245
                        }
 
246
                        
 
247
                        ShowMessage (txt);
 
248
                }
 
249
                
 
250
                void RunInstall ()
 
251
                {
 
252
                        try {
 
253
                                if (filesToInstall != null)
 
254
                                        service.Install (installMonitor, filesToInstall);
 
255
                                else
 
256
                                        service.Install (installMonitor, packagesToInstall);
 
257
                        } catch (Exception ex) {
 
258
                                installMonitor.Errors.Add (ex.Message);
 
259
                        } finally {
 
260
                                installMonitor.Dispose ();
 
261
                        }
 
262
                }
 
263
                
 
264
                void RunUninstall ()
 
265
                {
 
266
                        try {
 
267
                                service.Uninstall (installMonitor, uninstallIds);
 
268
                        } catch (Exception ex) {
 
269
                                installMonitor.Errors.Add (ex.Message);
 
270
                        } finally {
 
271
                                installMonitor.Dispose ();
 
272
                        }
 
273
                }
 
274
        }
 
275
}
 
276