Update called vala api.
[stuffkeeper.git] / src / stuffkeeper-data-link.gob
blob29434422cba373213b93aee7363f37c57f2bee6f
1 requires 2.0.10
3 %h{
4 #include <gtk/gtk.h>
5 #include "config.h"
6 #include "misc.h"
7 #include "stuffkeeper-data-backend.h"
8 #include "stuffkeeper-data-item.h"
9 %}
12 class Stuffkeeper:Data:Link from Gtk:Entry 
14     private StuffkeeperDataItem *item = {NULL};
15     private gchar *field = {NULL} destroywith g_free;
16     private gulong changed_item_entry = {0};
17     private gulong changed_entry = {0};
19     private guint changed_entry_timeout = {0};
20     private GdkColor writing ;
21     private GdkColor base;
22     private GdkColor text;
24     /* lock signal */
25     private gulong signal_backend_locked = {0};
27     private 
28     void
29     update_icon(self)
30     {
31         const gchar *text = gtk_entry_get_text(GTK_ENTRY(self));
32         UrlType type = url_type(text);
35         if(type == URL_EMAIL)
36         {
37             gtk_entry_set_icon_from_icon_name(GTK_ENTRY(self), GTK_ENTRY_ICON_PRIMARY,"internet-mail");
38         } else if ( type == URL_HTTP )
39         {
40             gtk_entry_set_icon_from_icon_name(GTK_ENTRY(self), GTK_ENTRY_ICON_PRIMARY,"internet-web-browser");
41         } else {
42             gtk_entry_set_icon_from_stock(GTK_ENTRY(self), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_JUMP_TO);
43         }
44     }
45     private
46     gboolean  
47     save_changes(self)
48     {
49         const gchar *text = gtk_entry_get_text(GTK_ENTRY(self));
50         g_object_ref(self);
52         gtk_widget_modify_text(GTK_WIDGET(self), GTK_STATE_NORMAL, &(self->_priv->text));
53         gtk_widget_modify_base(GTK_WIDGET(self), GTK_STATE_NORMAL, &(self->_priv->base));
54         self->_priv->changed_entry_timeout = 0;
56         self_update_icon(self);
57         if(self->_priv->field)
58         {
59             stuffkeeper_data_item_set_string(self->_priv->item, self->_priv->field, text);
60         }else{
61             stuffkeeper_data_item_set_title(self->_priv->item, text);
62         }
64         g_object_unref(self);
65         return FALSE;
66     }
67     public
68     void
69     changed(self, gpointer data)
70     {
71         if(self->_priv->changed_entry_timeout)
72         {
73             g_source_remove(self->_priv->changed_entry_timeout);
74         }
75         self->_priv->changed_entry_timeout = g_timeout_add(1000,(GSourceFunc)self_save_changes,self);
77         gtk_widget_modify_text(GTK_WIDGET(self), GTK_STATE_NORMAL, &(GTK_WIDGET(self)->style->black));
78         gtk_widget_modify_base(GTK_WIDGET(self), GTK_STATE_NORMAL, &(self->_priv->writing));
79     }
81     public
82     void
83     item_changed(self,const gchar *field, StuffkeeperDataItem *item)
84     {
85         const gchar *text = gtk_entry_get_text(GTK_ENTRY(self));
86         gchar *value = NULL;
87         if(self->_priv->field)
88         {
89             if(field && strcmp(field, self->_priv->field) == 0)
90             {
91                 value = stuffkeeper_data_item_get_string(item, self->_priv->field);
92             }
93         }else{
94             value = stuffkeeper_data_item_get_title(item); 
95         }
96         if(value)
97         {
98             if(strcmp(text,value))
99             {
100                 g_signal_handler_block(self,self->_priv->changed_entry);
101                 gtk_entry_set_text(GTK_ENTRY(self), value);
102                 self_update_icon(self);
103                 g_signal_handler_unblock(self,self->_priv->changed_entry);
104             }
105             g_free(value);
106         }
107     }
108     
109     /**
110      * link clicked
111      */
112      private
113      void
114      open_link(self, GtkEntry *entry)
115      {
116             const gchar *text = gtk_entry_get_text(GTK_ENTRY(self));
117             if(strlen(text) > 0)
118             {
119                 UrlType type = url_type(text);
120                 if(type == URL_EMAIL)
121                 {
122                     open_email(text);
123                 }
124                 else
125                 {
126                     open_url(text);
127                 }
128             }
129      }
131     private
132     void
133     link_icon_clicked(self, GtkEntryIconPosition icon_pos, GdkEventButton *event, GtkEntry *icon_entry)
134     {
135         if(event->button == 1 && icon_pos == GTK_ENTRY_ICON_PRIMARY)
136         {
137             self_open_link(self, GTK_ENTRY(icon_entry));
138         }
139     }
141     private 
142     void
143     style_set(self, GtkStyle *prev, GtkWidget *wid)
144     {
146         if(self->_priv->changed_entry_timeout == 0)
147         {
148             self->_priv->base = (GTK_WIDGET(self)->style->base[GTK_STATE_NORMAL]);
149             self->_priv->text = (GTK_WIDGET(self)->style->text[GTK_STATE_NORMAL]);
150         }
151     }
152     public
153     void
154     backend_locked(self, GParamSpec *arg1, StuffkeeperDataBackend *backend)
155     {
156         gboolean locked = stuffkeeper_data_backend_get_locked(backend);
157         gtk_editable_set_editable(GTK_EDITABLE(self), !locked);
158     }
160     public
161     GtkWidget *
162     new(StuffkeeperDataItem *item, const gchar *field)
163     {
164         Self *obj = GET_NEW;
165         gchar *value =NULL;
166         //GdkColor color;
168         obj->_priv->base = (GTK_WIDGET(obj)->style->base[GTK_STATE_NORMAL]);
169         obj->_priv->text = (GTK_WIDGET(obj)->style->text[GTK_STATE_NORMAL]);
171         obj->_priv->writing.red = 255*255;
172         obj->_priv->writing.green = 253*255;
173         obj->_priv->writing.blue = 197*255;
175         gdk_colormap_alloc_color(gtk_widget_get_default_colormap(), &(obj->_priv->writing), TRUE,TRUE);
177         
178   /*      color.blue = 65535;
179         color.red = color.green = 0;
180         gdk_colormap_alloc_color(gtk_widget_get_default_colormap(), &color, TRUE,TRUE);
181         gtk_widget_modify_text(GTK_WIDGET(obj), GTK_STATE_NORMAL, &color);
183         g_signal_connect_swapped(G_OBJECT(obj), "style-set", G_CALLBACK(self_style_set), obj);
186         gtk_entry_set_icon_from_stock(GTK_ENTRY(obj), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_JUMP_TO);
191         if(!g_key_file_get_boolean(config_file, "interface", "has-border",NULL))
192         gtk_entry_set_has_frame(GTK_ENTRY(obj), FALSE);
194         g_signal_connect_swapped(G_OBJECT(obj), "icon-release", G_CALLBACK(self_link_icon_clicked), obj);
196         /* store item */
197         obj->_priv->item = item; 
198         /* field */
199         obj->_priv->field = g_strdup(field);
200             
201         obj->_priv->changed_item_entry = g_signal_connect_swapped(G_OBJECT(item), "item-changed", G_CALLBACK(self_item_changed), obj);
203         /* update the entry */
204         if(obj->_priv->field)
205         {
206             value = stuffkeeper_data_item_get_string(item, obj->_priv->field);
207         }else{
208             value = stuffkeeper_data_item_get_title(item); 
209         }
210         if(value)
211         {
212             gtk_entry_set_text(GTK_ENTRY(obj), value);
213             g_free(value);
214         }
215         self_update_icon(obj);
216         
217         /* this is destroyed when self is destroyed, so no need to disconnect myself */
218         g_signal_connect_swapped(G_OBJECT(obj), "activate", G_CALLBACK(self_open_link), obj);
219         obj->_priv->changed_entry = g_signal_connect(G_OBJECT(obj), "changed", G_CALLBACK(self_changed), NULL);
221         obj->_priv->signal_backend_locked = g_signal_connect_swapped(G_OBJECT(stuffkeeper_data_item_get_backend(item)), 
222                                                                      "notify::locked", G_CALLBACK(self_backend_locked), obj);
223         self_backend_locked(obj,NULL, STUFFKEEPER_DATA_BACKEND(stuffkeeper_data_item_get_backend(item)));
226         return GTK_WIDGET(obj);
227     }
229     override (G:Object)
230         void
231         finalize (G:Object *obj)
232         {
233             Self *self = SELF(obj);
234             if(self->_priv->signal_backend_locked) {                                                                     
235                 g_signal_handler_disconnect(G_OBJECT(stuffkeeper_data_item_get_backend(self->_priv->item)), self->_priv->signal_backend_locked);
236                 self->_priv->signal_backend_locked = 0;
237             }
238             if(self->_priv->changed_item_entry)
239             {
240                 g_signal_handler_disconnect(self->_priv->item,self->_priv->changed_item_entry);
241                 self->_priv->changed_item_entry = 0;
242             }
243             if(self->_priv->changed_entry_timeout)
244             {
245                 g_source_remove(self->_priv->changed_entry_timeout);
246                 self->_priv->changed_entry_timeout = 0;
248                 self_save_changes(self);
249             }
250             PARENT_HANDLER(obj);
251         }