/+junk/libgego

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/libgego

« back to all changes in this revision

Viewing changes to src/gego_global_notify.c

  • Committer: Gustav Hartvigsson
  • Date: 2015-06-01 20:38:00 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150601203800-ymkb9tmijhd4dkby
* Initial code.
* Bulding does not work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "gego_global_notify.h"
 
2
 
 
3
static GegoGlobalNotify * _gego_global_notify_instance = NULL;
 
4
 
 
5
#define GEGO_GLOBAL_NOTIFY_GET_PRIVATE(o)\
 
6
  G_TYPE_INSTANCE_GET_PRIVATE (o, GEGO_TYPE_GLOBEL_NOTIFY, GegoGlobalNotifyPrivate)
 
7
 
 
8
/**
 
9
 * section: gego_global_notify
 
10
 * @title: GegoGlobalNotify
 
11
 * @short_description: subscriber based application wide notifications.
 
12
 *
 
13
 * The #GegoGlobalNotify tries to solve a probrem that occurs in many peaces of
 
14
 * software: How to notify many parts, or components, of changes, or how to
 
15
 * send information from one part of a program to many other parts the same
 
16
 * program
 
17
 **/
 
18
 
 
19
/**
 
20
 * GegoGlobalNotify:
 
21
 * @short_description: aoeu
 
22
 */
 
23
 
 
24
 
 
25
////////////////////////////////////////////////////////////////////////////////
 
26
 
 
27
struct GegoGlobalNotifyPrivate {
 
28
  /* <--- private ---> */
 
29
  GHashTable * hash_table; /* GHashTable<gchar *><GLinkedList<GegoGlobalNotifyCallbackItem*>*>* */
 
30
  
 
31
};
 
32
 
 
33
struct GegoCallbackItem {
 
34
  /* <--- private ---> */
 
35
  guint subscription_id;
 
36
  GQuark key_id;
 
37
  gpointer subscriber_data;
 
38
  GCallback callback;
 
39
};
 
40
 
 
41
G_DEFINE_TYPE_WITH_PRIVATE (GegoGlobalNotify, gego_global_notify, G_TYPE_OBJECT);
 
42
 
 
43
GegoGlobalNotify *
 
44
gego_global_notify_new (void) {
 
45
  GegoGlobalNotify * self = g_object_new (GEGO_TYPE_GLOBEL_NOTIFY, NULL, NULL);
 
46
  
 
47
  return self;
 
48
}
 
49
 
 
50
 
 
51
static GObject *
 
52
gego_global_notify_constructor (GType                   gtype,
 
53
                                guint                   n_properties,
 
54
                                GObjectConstructParam * properties) {
 
55
  static GObject * self = NULL;
 
56
  
 
57
  
 
58
  if (self == NULL) {
 
59
    self = G_OBJECT_CLASS (gego_global_notify_parent_class
 
60
                           )->constructor (gtype, n_properties, properties);
 
61
    g_object_add_weak_pointer (G_OBJECT (self), (gpointer) &self);
 
62
    return G_OBJECT (self);
 
63
  }
 
64
  
 
65
  return g_object_ref (G_OBJECT (self));
 
66
}
 
67
 
 
68
static void
 
69
gego_global_notify_class_init (GegoGlobalNotifyClass * klass) {
 
70
  GObjectClass * object_class = G_OBJECT_CLASS (klass);
 
71
  
 
72
  object_class->constructor = gego_global_notify_constructor;
 
73
  
 
74
}
 
75
 
 
76
 
 
77
 
 
78
static void
 
79
gego_global_notify_init (GegoGlobalNotify * self) {
 
80
  
 
81
  GegoGlobalNotifyPrivate * priv = GEGO_GLOBAL_NOTIFY_GET_PRIVATE (self);
 
82
  
 
83
  /* Note that the hash table should contain linked lists of
 
84
   * GegoGlobalNotifyCallbackItems.
 
85
   * In template speak:
 
86
   * GHashTable<gchar *><GLinkedList<GegoGlobalNotifyCallbackItem *> *>*
 
87
   */
 
88
  priv->hash_table = g_hash_table_new (g_str_hash, g_str_equal);
 
89
  
 
90
}
 
91
 
 
92
////////////////////////////////////////////////////////////////////////////////
 
93
 
 
94
/**
 
95
 * gego_global_notify_initalize:
 
96
 * @short_description: Should be run at the start of the program.
 
97
 *
 
98
 * @err: (allow-none): NULL'ed errer to be passed to function.
 
99
 */
 
100
gboolean
 
101
gego_global_notify_initalize (GError * err) {
 
102
  if (_gego_global_notify_instance != NULL) {
 
103
    err = g_error_new (GEGO_GLOBAL_NOTIFY_ERROR,
 
104
                 GEGO_GLOBAL_NOTIFY_ERROR_ALREADY_INITALIZED,
 
105
                 "The Global Notify system is already initialized.");
 
106
    return;
 
107
  }
 
108
  _gego_global_notify_instance = gego_global_notify_new ();
 
109
}
 
110
 
 
111
/**
 
112
 * gego_global_notify_unsubscribe:
 
113
 * @short_description: Sholud only be run when the program quits.
 
114
 *
 
115
 * Calling callbacks after this point is an undefined behaviour.
 
116
 *
 
117
  * @err: (allow-none):NULL'ed errer to be passed to function.
 
118
 */
 
119
gboolean
 
120
gego_global_notify_uninitalize (GError * err) {
 
121
  g_object_unref (_gego_global_notify_instance);
 
122
}
 
123
 
 
124
/**
 
125
 * gego_global_notify_add_notification:
 
126
 * 
 
127
 * Add a notifications to the system.
 
128
 * 
 
129
 * @name The name of the notification to be added.
 
130
 * @err: (allow-none):NULL'ed errer to be passed to function.
 
131
 * 
 
132
 * return: GUI for that callbacks. negative value on fail.
 
133
 */
 
134
gint
 
135
gego_global_notify_add_notification (gchar * name, GError * err) {
 
136
  return 0;
 
137
}
 
138
 
 
139
/**
 
140
 * gego_global_notify_remove_notification:
 
141
 * @short_description: removes a notification from the system.
 
142
 * 
 
143
 * 
 
144
 * @name: The name of the notification to be removed from the system.
 
145
 * @notification_id: The notification ID that was given when the notification
 
146
 * was added to the system.
 
147
 * @err: (allow-none):A NULL'ed error to be passed to the...
 
148
 */
 
149
void
 
150
gego_global_notify_remove_notification (gchar * name,
 
151
                                        gint notification_id,
 
152
                                        GError * err) {
 
153
  
 
154
}
 
155
 
 
156
/**
 
157
 * gego_global_notify_subscribe:
 
158
 * @short_description: hook up a callback to a signal name.
 
159
 *
 
160
 * @name: The name of the callback to attash to.
 
161
 * @callback: the callback to hookup to the signal.
 
162
 *
 
163
 *
 
164
 * This function is used to hook up a callback to a globla signal. It is
 
165
 * is recomended to save the retuned value for when a callback should be
 
166
 * unsubscribed to, using the gego_global_notify_unsubscribe() function.
 
167
 * 
 
168
 * Here is an example of a callback.
 
169
 * \[<!-- language="C" -->
 
170
 * 
 
171
 * \]
 
172
 * 
 
173
 * returns: the subscription id of the signal.
 
174
 */
 
175
gint
 
176
gego_global_notify_subscribe (gchar * name, GCallback callback, gpointer subscriber_data, GError * err);
 
177
 
 
178
/**
 
179
 * gego_global_notify_unsubscribe:
 
180
 * @short_description: removes a callback from a 
 
181
 */
 
182
void
 
183
gego_global_notify_unsubscribe (gchar * name, gint subscription_id, GError * err);
 
184
 
 
185
gint
 
186
gego_global_notify_call (gchar * name, gpointer caller_data, GError * err);
 
187
 
 
188
/**
 
189
 * gego_global_notify_is_initialized:
 
190
 * @short_description: Check if Global Notify is initialized.
 
191
 *
 
192
 * return: #TRUE if Global Notify is initialized, otherwise #FALSE
 
193
 */
 
194
gboolean
 
195
gego_global_notify_is_initialized (void);
 
196
 
 
197
/**
 
198
 * gego_global_notify_error_quark: (skip)
 
199
 */
 
200
GQuark
 
201
gego_global_notify_error_quark (void) {
 
202
  return g_quark_from_static_string ("gego-global-notify-error-quark");
 
203
}