~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/ManageSitesDialog.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
// ManageSitesDialog.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using Gtk;
 
32
using Mono.Unix;
 
33
using System.Threading;
 
34
 
 
35
using Mono.Addins.Setup;
 
36
 
 
37
 
 
38
namespace Mono.Addins.Gui
 
39
{
 
40
        partial class ManageSitesDialog : Dialog
 
41
        {
 
42
                ListStore treeStore;
 
43
                SetupService service;
 
44
                
 
45
                public ManageSitesDialog (Gtk.Window parent, SetupService service)
 
46
                {
 
47
                        Build ();
 
48
                        TransientFor = parent;
 
49
                        Services.PlaceDialog (this, parent);
 
50
                        this.service = service;
 
51
                        treeStore = new Gtk.ListStore (typeof (string), typeof (string), typeof(bool));
 
52
                        repoTree.Model = treeStore;
 
53
                        repoTree.HeadersVisible = false;
 
54
                        var crt = new Gtk.CellRendererToggle ();
 
55
                        crt.Toggled += HandleRepoToggled;
 
56
                        repoTree.AppendColumn ("", crt, "active", 2);
 
57
                        repoTree.AppendColumn ("", new Gtk.CellRendererText (), "markup", 1);
 
58
                        repoTree.Selection.Changed += new EventHandler(OnSelect);
 
59
                        
 
60
                        AddinRepository[] reps = service.Repositories.GetRepositories ();
 
61
                        foreach (AddinRepository rep in reps)
 
62
                                AppendRepository (rep);
 
63
 
 
64
                        btnRemove.Sensitive = false;
 
65
                }
 
66
 
 
67
                public override void Dispose ()
 
68
                {
 
69
                        base.Dispose ();
 
70
                        Destroy ();
 
71
                }
 
72
                
 
73
                void AppendRepository (AddinRepository rep)
 
74
                {
 
75
                        string txt = GLib.Markup.EscapeText (rep.Title) + "\n<span color='darkgray'>" + GLib.Markup.EscapeText (rep.Url) + "</span>";
 
76
                        treeStore.AppendValues (rep.Url, txt, rep.Enabled);
 
77
                }
 
78
                
 
79
                protected void OnAdd (object sender, EventArgs e)
 
80
                {
 
81
                        NewSiteDialog dlg = new NewSiteDialog (this);
 
82
                        try {
 
83
                                if (dlg.Run ()) {
 
84
                                        string url = dlg.Url;
 
85
                                        if (!url.StartsWith ("http://") && !url.StartsWith ("https://") && !url.StartsWith ("file://")) {
 
86
                                                url = "http://" + url;
 
87
                                        }
 
88
                                        
 
89
                                        try {
 
90
                                                new Uri (url);
 
91
                                        } catch {
 
92
                                                Services.ShowError (null, "Invalid url: " + url, null, true);
 
93
                                        }
 
94
                                        
 
95
                                        if (!service.Repositories.ContainsRepository (url)) {
 
96
                                                ProgressDialog pdlg = new ProgressDialog (this);
 
97
                                                pdlg.Show ();
 
98
                                                pdlg.SetMessage (AddinManager.CurrentLocalizer.GetString ("Registering repository"));
 
99
                                                
 
100
                                                bool done = false;
 
101
                                                AddinRepository rr = null;
 
102
                                                Exception error = null;
 
103
                                                
 
104
                                                ThreadPool.QueueUserWorkItem (delegate {
 
105
                                                        try {
 
106
                                                                rr = service.Repositories.RegisterRepository (pdlg, url, true);
 
107
                                                        } catch (System.Exception ex) {
 
108
                                                                error = ex;
 
109
                                                        } finally {
 
110
                                                                done = true;
 
111
                                                        }
 
112
                                                });
 
113
                                                
 
114
                                                while (!done) {
 
115
                                                        if (Gtk.Application.EventsPending ())
 
116
                                                                Gtk.Application.RunIteration ();
 
117
                                                        else
 
118
                                                                Thread.Sleep (100);
 
119
                                                }
 
120
 
 
121
                                                pdlg.Destroy ();
 
122
                                                
 
123
                                                if (pdlg.HadError) {
 
124
                                                        if (rr != null)
 
125
                                                                service.Repositories.RemoveRepository (rr.Url);
 
126
                                                        return;
 
127
                                                }
 
128
                                                
 
129
                                                if (error != null) {
 
130
                                                        Services.ShowError (error, "The repository could not be registered", null, true);
 
131
                                                        return;
 
132
                                                }
 
133
                                                
 
134
                                                AppendRepository (rr);
 
135
                                        }
 
136
                                }
 
137
                        } finally {
 
138
                                dlg.Destroy ();
 
139
                        }
 
140
                }
 
141
                
 
142
                protected void OnRemove (object sender, EventArgs e)
 
143
                {
 
144
                        Gtk.TreeModel foo;
 
145
                        Gtk.TreeIter iter;
 
146
                        if (!repoTree.Selection.GetSelected (out foo, out iter))
 
147
                                return;
 
148
                                
 
149
                        string rep = (string) treeStore.GetValue (iter, 0);
 
150
                        service.Repositories.RemoveRepository (rep);
 
151
                        
 
152
                        treeStore.Remove (ref iter);
 
153
                }
 
154
 
 
155
                void HandleRepoToggled (object o, ToggledArgs args)
 
156
                {
 
157
                        Gtk.TreeIter iter;
 
158
                        if (!treeStore.GetIterFromString (out iter, args.Path))
 
159
                                return;
 
160
                        
 
161
                        bool newVal = !(bool) treeStore.GetValue (iter, 2);
 
162
                        string rep = (string) treeStore.GetValue (iter, 0);
 
163
                        service.Repositories.SetRepositoryEnabled (rep, newVal);
 
164
                        
 
165
                        treeStore.SetValue (iter, 2, newVal);
 
166
                }
 
167
                
 
168
                protected void OnSelect(object sender, EventArgs e)
 
169
                {
 
170
                        btnRemove.Sensitive = repoTree.Selection.CountSelectedRows() > 0;
 
171
                }
 
172
        }
 
173
}