[blam/gtk-builder] Dialogs: move some common code to a base class



commit 358293ee55df15e7552fa0f01b14633f51b72853
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Wed Oct 9 10:54:09 2013 +0200

    Dialogs: move some common code to a base class

 src/Blam.cs        |   11 +++--
 src/ChannelList.cs |   44 ++++++++++++------
 src/Dialogs.cs     |  123 +++++++++++++++++++++++++++------------------------
 3 files changed, 100 insertions(+), 78 deletions(-)
---
diff --git a/src/Blam.cs b/src/Blam.cs
index 9dfbc79..bb779c8 100644
--- a/src/Blam.cs
+++ b/src/Blam.cs
@@ -79,11 +79,12 @@ namespace Blam
 
                        item = bld.GetObject<MenuItem>("menu-add-group");
                        item.ObserveActivated().Subscribe(obj => {
-                               var dialog = new AddGroupDialog(MainWindow);
-                               if (dialog.Run()) {
-                                       var grp = new ChannelGroup();
-                                       grp.Name = dialog.Name;
-                                       channels.Add(grp);
+                               using (var dialog = new AddGroupDialog(MainWindow)) {
+                                       if (dialog.Run()) {
+                                               var grp = new ChannelGroup();
+                                               grp.Name = dialog.Name;
+                                               channels.Add(grp);
+                                       }
                                }
                        });
                }
diff --git a/src/ChannelList.cs b/src/ChannelList.cs
index 0fab122..443798a 100644
--- a/src/ChannelList.cs
+++ b/src/ChannelList.cs
@@ -79,18 +79,17 @@ namespace Blam
                        var removals = changes.Where(x => x.EventArgs.Action == 
NotifyCollectionChangedAction.Remove)
                                .SelectMany(x => enumerateChannels(x.EventArgs.OldItems));
 
+                       additions.Subscribe(chan => {
+                               Gtk.TreeIter iter = (model as TreeStore).AppendValues(chan);
+                               chan.Iter = iter;
+                       });
+
                        // let's not wire this up until we know how to deal with arbitrary removals
-                       //additions.Subscribe(chan => {
-                       //      Gtk.TreeIter iter = (model as TreeStore).AppendValues(chan);
-                       //      chan.Iter = iter;
-                       //
+                       //removals.Subscribe(chan => {
+                       //      var iter = chan.Iter;
+                       //      (model as TreeStore).Remove(ref iter);
                        //});
 
-                       removals.Subscribe(chan => {
-                               var iter = chan.Iter;
-                               (model as TreeStore).Remove(ref iter);
-                       });
-
                        foreach (Channel chan in channels) {
                                chan.Iter = model.AppendValues(chan);
                                chan.PropertyChanged += OnPropertyChanged;
@@ -119,15 +118,30 @@ namespace Blam
                                if (chan is ChannelGroup)
                                        return;
 
-                               var diag = new ChannelDialog();
-                               diag.Run((Channel)chan);
+                               Channel channel = (Channel) chan;
+                               using (var dialog = new ChannelDialog(MainWindow, channel))
+                               {
+                                       if (dialog.Run() == true) {
+                                               channel.Name = dialog.Name;
+                                               channel.Url = dialog.Url;
+                                               channel.http_username = dialog.Username;
+                                               channel.http_password = dialog.Password;
+                                       }
+                               }
                        };
                        Popup.RemoveSelected += () => {
                                var chan = GetSelected();
-                               var del = new RemoveChannelDialog(MainWindow).Run(chan);
-                               if (del) {
-                                       var iter = chan.Iter;
-                                       model.Remove(ref iter);
+                               using (var dialog = new RemoveChannelDialog(MainWindow, chan))
+                               {
+                                       if (dialog.Run()) {
+                                               var iter = chan.Iter;
+                                               if (chan is Channel)
+                                                       channels.Remove((Channel)chan);
+                                               else
+                                                       groups.Remove((ChannelGroup)chan);
+
+                                               model.Remove(ref iter);
+                                       }
                                }
                        };
                }
diff --git a/src/Dialogs.cs b/src/Dialogs.cs
index c8897d9..db36cf0 100644
--- a/src/Dialogs.cs
+++ b/src/Dialogs.cs
@@ -20,31 +20,54 @@ namespace Blam
        using IChannel = Imendio.Blam.IChannel;
        using Channel = Imendio.Blam.Channel;
 
-       class RemoveChannelDialog
+       class Modal : IDisposable
        {
-               Dialog Dialog;
-               Label Label;
+               protected Dialog dialog;
+               protected Builder bld;
 
-               public RemoveChannelDialog(Gtk.Window parent)
+               public Modal()
                {
-                       var bld = new Builder();
+               }
+
+               public Modal(Gtk.Window parent, string name)
+               {
+                       bld = new Builder();
                        bld.AddFromResource("dialogs.ui");
 
-                       Dialog = bld.GetObject<Dialog>("remove-channel");
-                       Dialog.TransientFor = parent;
-                       Dialog.IconName = "blam";
-                       Label = bld.GetObject<Label>("remove-channel-label");
+                       dialog = bld.GetObject<Dialog>(name);
+                       if (dialog == null)
+                               throw new ArgumentException(String.Format("No such dialog {0}", name));
+
+                       dialog.TransientFor = parent;
+                       dialog.IconName = "blam";
                }
 
-               public bool Run(IChannel chan)
+               public ResponseType Run()
                {
-                       var text = String.Format(_("Do you want to remove the channel or group <b>{0}</b> 
from the channel list?"), chan.Name);
-                       Label.Markup = text;
+                       var res = dialog.Run();
+                       dialog.Hide();
 
-                       var res = Dialog.Run();
-                       Dialog.Hide();
+                       return (ResponseType)res;
+               }
 
-                       return res == (int)ResponseType.Accept;
+               public void Dispose()
+               {
+                       dialog.Dispose();
+               }
+       }
+
+       class RemoveChannelDialog : Modal
+       {
+               public RemoveChannelDialog(Gtk.Window parent, IChannel chan)
+                       : base(parent, "remove-channel")
+               {
+                       var label = bld.GetObject<Label>("remove-channel-label");
+                       label.Markup = String.Format(_("Do you want to remove the channel or group <b>{0}</b> 
from the channel list?"), chan.Name);
+               }
+
+               public new bool Run()
+               {
+                       return base.Run() == ResponseType.Accept;
                }
 
                static string _(string msg)
@@ -53,7 +76,7 @@ namespace Blam
                }
        }
 
-       public class AddChannelDialog : IDisposable
+       class AddChannelDialog : Modal
        {
                public string Username {
                        get { return usernameEntry.Text; }
@@ -71,7 +94,6 @@ namespace Blam
                Entry urlEntry;
                Entry usernameEntry;
                Entry passwordEntry;
-               Dialog dialog;
 
                IDisposable okSensitive;
                IDisposable activation;
@@ -86,13 +108,8 @@ namespace Blam
                }
 
                public AddChannelDialog(Gtk.Window parent)
+                       : base(parent, "add-channel")
                {
-                       var bld = new Builder();
-                       bld.AddFromResource("dialogs.ui");
-
-                       dialog = bld.GetObject<Dialog>("add-channel");
-                       dialog.TransientFor = parent;
-
                        urlEntry = bld.GetObject<Entry>("add-channel-url");
                        usernameEntry = bld.GetObject<Entry>("add-channel-username");
                        passwordEntry = bld.GetObject<Entry>("add-channel-password");
@@ -113,24 +130,21 @@ namespace Blam
                                .Subscribe(obj => button.Click());
                }
 
-               public bool Run()
+               public new bool Run()
                {
-                       var res = dialog.Run();
-                       dialog.Hide();
-
-                       return res == (int)ResponseType.Ok;
+                       return base.Run() == ResponseType.Ok;
                }
 
-               public void Dispose()
+               public new void Dispose()
                {
+                       base.Dispose();
                        okSensitive.Dispose();
                        activation.Dispose();
                }
        }
 
-       public class ChannelDialog
+       class ChannelDialog : Modal
        {
-               Dialog dialog;
                Entry  nameEntry     = null;
                Entry  urlEntry      = null;
                Entry  usernameEntry = null;
@@ -140,13 +154,14 @@ namespace Blam
                IDisposable activationSub;
                IDisposable editsSub;
 
-               public ChannelDialog()
-               {
-                       var bld = new Builder();
-                       bld.AddFromResource("dialogs.ui");
-
-                       dialog = bld.GetObject<Dialog>("edit-channel");
+               public string Url { get { return urlEntry.Text; } }
+               public string Name { get { return nameEntry.Text; } }
+               public string Username { get { return usernameEntry.Text; } }
+               public string Password { get { return passwordEntry.Text; } }
 
+               public ChannelDialog(Gtk.Window parent, Channel channel)
+                       : base(parent, "edit-channel")
+               {
                        var image = bld.GetObject<Gtk.Image>("edit-channel-image");
                        image.Pixbuf = Pixbuf.LoadFromResource("blam-edit-news.png");
 
@@ -155,15 +170,16 @@ namespace Blam
                        usernameEntry = bld.GetObject<Entry>("edit-channel-username");
                        passwordEntry = bld.GetObject<Entry>("edit-channel-password");
                        applyButton = bld.GetObject<Button>("edit-channel-apply");
-               }
 
-               public void Run(Channel channel)
-               {
                        nameEntry.Text = channel.Name;
                        urlEntry.Text = channel.Url;
                        usernameEntry.Text = channel.http_username;
                        passwordEntry.Text = channel.http_password;
 
+               }
+
+               public new bool Run()
+               {
                        var activations = Observable.Merge(nameEntry.OnActivated(), urlEntry.OnActivated(),
                                                           usernameEntry.OnActivated(), 
passwordEntry.OnActivated())
                                .Where(x => applyButton.Sensitive);
@@ -174,24 +190,21 @@ namespace Blam
                                applyButton.Sensitive = !String.IsNullOrEmpty(nameEntry.Text) && 
!String.IsNullOrEmpty(urlEntry.Text);
                        });
 
-                       var res = dialog.Run();
-                       dialog.Hide();
+                       var res = base.Run() == ResponseType.Apply;
                        activationSub.Dispose();
                        editsSub.Dispose();
 
-                       if (res != (int)ResponseType.Apply)
-                               return;
+                       return res;
+               }
 
-                       channel.Url = urlEntry.Text;
-                       channel.Name = nameEntry.Text;
-                       channel.http_username = usernameEntry.Text;
-                       channel.http_password = passwordEntry.Text;
+               public new void Dispose()
+               {
+                       base.Dispose();
                }
        }
 
-       public class AddGroupDialog
+       class AddGroupDialog : Modal
        {
-               Dialog dialog;
                Entry name;
                Button button;
 
@@ -205,21 +218,15 @@ namespace Blam
                }
 
                public AddGroupDialog(Gtk.Window parent)
+                       : base(parent, "add-group")
                {
-                       var bld = new Builder();
-                       bld.AddFromResource("dialogs.ui");
-
-                       dialog = bld.GetObject<Dialog>("add-group");
-                       dialog.TransientFor = parent;
-                       dialog.IconName = "blam";
-
                        button = bld.GetObject<Button>("add-group-ok");
                        name = bld.GetObject<Entry>("add-group-name");
                        clickable = name.ObserveTextChanged().Select(t => 
!String.IsNullOrEmpty(t)).StartWith(false);
                        activation = name.OnActivated().Where(_ => button.Sensitive);
                }
 
-               public bool Run()
+               public new bool Run()
                {
                        var clickSub = clickable.Subscribe(v => button.Sensitive = v);
                        var actSub = activation.Subscribe(_ => button.Click());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]