[blam/gtk-builder] Create an active ChannelDialog



commit 3b91a515cdf1366d517e92cfa5e120e9798facc1
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Mon Oct 7 14:46:13 2013 +0200

    Create an active ChannelDialog
    
    Move it into Dialogs.cs and make it act.

 src/ChannelDialog.cs   |   26 -------------------
 src/ChannelList.cs     |    2 +-
 src/Dialogs.cs         |   64 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/EntryExtensions.cs |    8 ++++++
 src/dialogs.ui         |   18 ++++++-------
 5 files changed, 81 insertions(+), 37 deletions(-)
---
diff --git a/src/ChannelDialog.cs b/src/ChannelDialog.cs
index 6df152a..55d513b 100644
--- a/src/ChannelDialog.cs
+++ b/src/ChannelDialog.cs
@@ -11,32 +11,6 @@ using Glade;
 using System;
 using System.Text;
 
-namespace Blam
-{
-       using Channel = Imendio.Blam.Channel;
-
-       public class ChannelDialog
-       {
-               Dialog Dialog;
-
-               public ChannelDialog()
-               {
-                       var bld = new Builder();
-                       bld.AddFromResource("dialogs.ui");
-
-                       Dialog = bld.GetObject<Dialog>("edit-channel");
-
-                       var image = bld.GetObject<Gtk.Image>("edit-channel-image");
-                       image.Pixbuf = Pixbuf.LoadFromResource("blam-edit-news.png");
-               }
-
-               public void Show (Channel channel)
-               {
-                       Dialog.ShowAll();
-               }
-       }
-}
-
 namespace Imendio.Blam {
     public class ChannelDialog {
     [Widget] Gtk.Dialog channelDialog = null;
diff --git a/src/ChannelList.cs b/src/ChannelList.cs
index 471a98b..fafd99a 100644
--- a/src/ChannelList.cs
+++ b/src/ChannelList.cs
@@ -89,7 +89,7 @@ namespace Blam
                                        return;
 
                                var diag = new ChannelDialog();
-                               diag.Show((Channel)chan);
+                               diag.Run((Channel)chan);
                        };
                        Popup.RemoveSelected += () => {
                                var chan = GetSelected();
diff --git a/src/Dialogs.cs b/src/Dialogs.cs
index c1f9966..5bd2b1e 100644
--- a/src/Dialogs.cs
+++ b/src/Dialogs.cs
@@ -18,6 +18,7 @@ namespace Blam
 {
        using ChannelCollection = Imendio.Blam.ChannelCollection;
        using IChannel = Imendio.Blam.IChannel;
+       using Channel = Imendio.Blam.Channel;
 
        class RemoveChannelDialog
        {
@@ -95,6 +96,8 @@ namespace Blam
                        usernameEntry = bld.GetObject<Entry>("add-channel-username");
                        passwordEntry = bld.GetObject<Entry>("add-channel-password");
                        var button = bld.GetObject<Button>("add-channel-ok");
+                       var image = bld.GetObject<Gtk.Image>("add-channel-image");
+                       image.Pixbuf = Pixbuf.LoadFromResource("blam-add-news.png");
 
                        var clipboard = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", true));
                        clipboard.RequestText((c, text) => {
@@ -133,6 +136,67 @@ namespace Blam
                        activation.Dispose();
                }
        }
+
+       public class ChannelDialog
+       {
+               Dialog dialog;
+               Entry  nameEntry     = null;
+               Entry  urlEntry      = null;
+               Entry  usernameEntry = null;
+               Entry  passwordEntry = null;
+               Button applyButton      = null;
+
+               IDisposable activationSub;
+               IDisposable editsSub;
+
+               public ChannelDialog()
+               {
+                       var bld = new Builder();
+                       bld.AddFromResource("dialogs.ui");
+
+                       dialog = bld.GetObject<Dialog>("edit-channel");
+
+                       var image = bld.GetObject<Gtk.Image>("edit-channel-image");
+                       image.Pixbuf = Pixbuf.LoadFromResource("blam-edit-news.png");
+
+                       nameEntry = bld.GetObject<Entry>("edit-channel-name");
+                       urlEntry = bld.GetObject<Entry>("edit-channel-url");
+                       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;
+
+                       var activations = Observable.Merge(nameEntry.OnActivated(), urlEntry.OnActivated(),
+                                                          usernameEntry.OnActivated(), 
passwordEntry.OnActivated())
+                               .Where(x => applyButton.Sensitive);
+                       activationSub = activations.Subscribe((obj) => applyButton.Click());
+
+                       var edits = Observable.Merge(nameEntry.OnChanged(), urlEntry.OnChanged());
+                       editsSub = edits.Subscribe(obj => {
+                               applyButton.Sensitive = !String.IsNullOrEmpty(nameEntry.Text) && 
!String.IsNullOrEmpty(urlEntry.Text);
+                       });
+
+                       var res = dialog.Run();
+                       dialog.Hide();
+                       activationSub.Dispose();
+                       editsSub.Dispose();
+
+                       if (res != (int)ResponseType.Apply)
+                               return;
+
+                       channel.Url = urlEntry.Text;
+                       channel.Name = nameEntry.Text;
+                       channel.http_username = usernameEntry.Text;
+                       channel.http_password = passwordEntry.Text;
+               }
+       }
 }
 
 namespace Imendio.Blam {
diff --git a/src/EntryExtensions.cs b/src/EntryExtensions.cs
index 0265e52..d3f31f9 100644
--- a/src/EntryExtensions.cs
+++ b/src/EntryExtensions.cs
@@ -12,6 +12,14 @@ namespace Blam
                                x => entry.Activated += x,
                                x => entry.Activated -= x);
                }
+
+               public static IObservable<EventPattern<EventArgs>> OnChanged(this Gtk.Entry entry)
+               {
+                       return Observable.FromEventPattern<EventHandler, EventArgs>(
+                               x => entry.Changed += x,
+                               x => entry.Changed -= x);
+               }
+
        }
 }
 
diff --git a/src/dialogs.ui b/src/dialogs.ui
index 160fd22..6fd95fe 100644
--- a/src/dialogs.ui
+++ b/src/dialogs.ui
@@ -252,8 +252,8 @@
               </packing>
             </child>
             <child>
-              <object class="GtkButton" id="edit-channel-ok">
-                <property name="label">gtk-ok</property>
+              <object class="GtkButton" id="edit-channel-apply">
+                <property name="label">gtk-apply</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">True</property>
@@ -277,7 +277,6 @@
         <child>
           <object class="GtkHBox" id="hbox1">
             <property name="visible">True</property>
-            <property name="sensitive">False</property>
             <property name="can_focus">False</property>
             <child>
               <object class="GtkImage" id="edit-channel-image">
@@ -296,7 +295,6 @@
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="label" translatable="yes">Change the fields to update channel 
information</property>
-                <property name="single_line_mode">True</property>
               </object>
               <packing>
                 <property name="expand">True</property>
@@ -358,7 +356,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkEntry" id="entry1">
+              <object class="GtkEntry" id="edit-channel-name">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="invisible_char">●</property>
@@ -374,7 +372,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkEntry" id="entry2">
+              <object class="GtkEntry" id="edit-channel-url">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="invisible_char">●</property>
@@ -392,7 +390,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkEntry" id="entry3">
+              <object class="GtkEntry" id="edit-channel-username">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="invisible_char">●</property>
@@ -410,7 +408,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkEntry" id="entry4">
+              <object class="GtkEntry" id="edit-channel-password">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="invisible_char">●</property>
@@ -437,8 +435,8 @@
       </object>
     </child>
     <action-widgets>
-      <action-widget response="0">edit-channel-cancel</action-widget>
-      <action-widget response="0">edit-channel-ok</action-widget>
+      <action-widget response="-6">edit-channel-cancel</action-widget>
+      <action-widget response="-10">edit-channel-apply</action-widget>
     </action-widgets>
   </object>
   <object class="GtkDialog" id="remove-channel">


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