| 1 |
/* Copyright 2011 by Yasuhiro Matsumoto |
| 2 |
* modification, are permitted provided that the following conditions are met: |
| 3 |
* |
| 4 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 5 |
* this list of conditions and the following disclaimer. |
| 6 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 7 |
* this list of conditions and the following disclaimer in the documentation |
| 8 |
* and/or other materials provided with the distribution. |
| 9 |
* |
| 10 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 11 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 12 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 13 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 14 |
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 15 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 20 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 21 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
#ifdef _WIN32 |
| 24 |
# include <ws2tcpip.h> |
| 25 |
#endif |
| 26 |
#include <stddef.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <ctype.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <getopt.h> |
| 35 |
#include <errno.h> |
| 36 |
|
| 37 |
#include <gtk/gtk.h> |
| 38 |
#ifdef _WIN32 |
| 39 |
# include <gdk/gdkwin32.h> |
| 40 |
#else |
| 41 |
# include <sys/socket.h> |
| 42 |
# include <netinet/tcp.h> |
| 43 |
# include <netinet/in.h> |
| 44 |
# include <arpa/inet.h> |
| 45 |
# include <netdb.h> |
| 46 |
# include <unistd.h> |
| 47 |
#endif |
| 48 |
#include <sqlite3.h> |
| 49 |
#ifdef _WIN32 |
| 50 |
# include <io.h> |
| 51 |
#endif |
| 52 |
#include <openssl/md5.h> |
| 53 |
#include <openssl/sha.h> |
| 54 |
#include <openssl/aes.h> |
| 55 |
#include <openssl/des.h> |
| 56 |
|
| 57 |
#include "gol.h" |
| 58 |
#include "compatibility.h" |
| 59 |
|
| 60 |
#ifdef HAVE_APP_INDICATOR |
| 61 |
#include <libappindicator/app-indicator.h> |
| 62 |
#endif |
| 63 |
|
| 64 |
#define GNTP_OK_STRING_LITERAL(_version, _action) \ |
| 65 |
"GNTP/" _version " -OK NONE\r\n" \ |
| 66 |
"Response-Action: " _action "\r\n\r\n" \ |
| 67 |
|
| 68 |
#define GNTP_ERROR_STRING_LITERAL(_version, _message, _desc) \ |
| 69 |
"GNTP/" _version " -ERROR " _message "\r\n" \ |
| 70 |
"Error-Description: " _desc "\r\n\r\n" \ |
| 71 |
|
| 72 |
typedef struct { |
| 73 |
void* handle; |
| 74 |
gboolean (*init)(); |
| 75 |
gboolean (*start)(); |
| 76 |
gboolean (*stop)(); |
| 77 |
gboolean (*term)(); |
| 78 |
const gchar* (*name)(); |
| 79 |
const gchar* (*description)(); |
| 80 |
gchar** (*thumbnail)(); |
| 81 |
void (*set_param)(const gchar*); |
| 82 |
gchar* (*get_param)(); |
| 83 |
} SUBSCRIBE_PLUGIN; |
| 84 |
|
| 85 |
typedef struct { |
| 86 |
void* handle; |
| 87 |
gboolean (*init)(); |
| 88 |
gboolean (*show)(NOTIFICATION_INFO* ni); |
| 89 |
gboolean (*term)(); |
| 90 |
const gchar* (*name)(); |
| 91 |
const gchar* (*description)(); |
| 92 |
gchar** (*thumbnail)(); |
| 93 |
void (*set_param)(const gchar*); |
| 94 |
gchar* (*get_param)(); |
| 95 |
} DISPLAY_PLUGIN; |
| 96 |
|
| 97 |
typedef enum |
| 98 |
{ |
| 99 |
GOL_STATUS_NORMAL = 0, |
| 100 |
GOL_STATUS_DND, |
| 101 |
} status_t; |
| 102 |
|
| 103 |
static gchar* password; |
| 104 |
static gboolean require_password_for_local_apps = FALSE; |
| 105 |
static gboolean require_password_for_lan_apps = FALSE; |
| 106 |
static sqlite3 *db; |
| 107 |
#ifdef HAVE_APP_INDICATOR |
| 108 |
static AppIndicator* indicator; |
| 109 |
#else |
| 110 |
static GtkStatusIcon* status_icon; |
| 111 |
#endif |
| 112 |
static GdkPixbuf* status_icon_pixbuf, * status_icon_dnd_pixbuf; |
| 113 |
static status_t gol_status; |
| 114 |
static GtkWidget* popup_menu; |
| 115 |
static GtkWidget* setting_dialog; |
| 116 |
static GtkWidget* about_dialog; |
| 117 |
static GList* display_plugins; |
| 118 |
static GList* subscribe_plugins; |
| 119 |
static DISPLAY_PLUGIN* current_display; |
| 120 |
static gchar* exepath; |
| 121 |
static SUBSCRIPTOR_CONTEXT sc; |
| 122 |
|
| 123 |
#ifndef LIBDIR |
| 124 |
# define LIBDIR exepath |
| 125 |
#endif |
| 126 |
#ifndef DATADIR |
| 127 |
# define DATADIR exepath |
| 128 |
#endif |
| 129 |
|
| 130 |
static const char* |
| 131 |
skipsp(const char* str) { |
| 132 |
for (; isspace(*str); ++str); |
| 133 |
return str; |
| 134 |
} |
| 135 |
|
| 136 |
static void* |
| 137 |
safely_realloc(void* ptr, const size_t newsize) { |
| 138 |
void* const tmp = realloc(ptr, newsize); |
| 139 |
if (!tmp) { |
| 140 |
perror("realloc"); |
| 141 |
free(ptr); |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
return tmp; |
| 145 |
} |
| 146 |
|
| 147 |
static size_t |
| 148 |
read_all(int fd, char** ptr) { |
| 149 |
const struct timeval timeout = { |
| 150 |
.tv_sec = 1, |
| 151 |
.tv_usec = 0, |
| 152 |
}; |
| 153 |
|
| 154 |
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t*) &timeout, sizeof(timeout)); |
| 155 |
|
| 156 |
const size_t len = 1024; |
| 157 |
size_t bufferlen = len; |
| 158 |
char* buf = (char*) malloc(bufferlen + 1); |
| 159 |
if (!buf) { |
| 160 |
perror("malloc"); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
char* end = buf; |
| 165 |
ptrdiff_t datalen = 0; |
| 166 |
int retry = 3; |
| 167 |
while (1) { |
| 168 |
ssize_t r = recv(fd, end, bufferlen - datalen, 0); |
| 169 |
if (r <= 0) { |
| 170 |
if (--retry < 0) break; |
| 171 |
#ifdef _WIN32 |
| 172 |
DWORD err = GetLastError(); |
| 173 |
if (err == WSAEWOULDBLOCK) continue; |
| 174 |
#else |
| 175 |
int err = errno; |
| 176 |
if (err == EWOULDBLOCK || err == EAGAIN) continue; |
| 177 |
#endif |
| 178 |
break; |
| 179 |
} |
| 180 |
*(end += r) = '\0'; |
| 181 |
datalen += r; |
| 182 |
if (r >= 4 && !strncmp(end - 4, "\r\n\r\n", 4)) break; |
| 183 |
|
| 184 |
bufferlen += len; |
| 185 |
buf = (char*) safely_realloc(buf, bufferlen + 1); |
| 186 |
if (!buf) return 0; |
| 187 |
end = buf + datalen; |
| 188 |
} |
| 189 |
*ptr = buf; |
| 190 |
return (ptrdiff_t) (end - buf); |
| 191 |
} |
| 192 |
|
| 193 |
unsigned int |
| 194 |
unhex(unsigned char c) { |
| 195 |
if (!isxdigit(c)) return 0; |
| 196 |
const char str[] = {c, '\0'}; |
| 197 |
char* _unused_endptr; |
| 198 |
return strtol(str, &_unused_endptr, 16); |
| 199 |
} |
| 200 |
|
| 201 |
DISPLAY_PLUGIN* |
| 202 |
find_display_plugin_or(bool(* pred)(const DISPLAY_PLUGIN*), DISPLAY_PLUGIN* const or_dp) { |
| 203 |
gint |
| 204 |
wrapped_pred(gconstpointer dp, gconstpointer GOL_UNUSED_ARG(user_data)) { |
| 205 |
return pred((const DISPLAY_PLUGIN*) dp) ? 0 : 1; |
| 206 |
} |
| 207 |
GList* elem = g_list_find_custom(display_plugins, NULL, wrapped_pred); |
| 208 |
return elem ? (DISPLAY_PLUGIN*) elem->data : or_dp; |
| 209 |
} |
| 210 |
|
| 211 |
DISPLAY_PLUGIN* |
| 212 |
find_display_plugin(bool(* pred)(const DISPLAY_PLUGIN*)) { |
| 213 |
return find_display_plugin_or(pred, NULL); |
| 214 |
} |
| 215 |
|
| 216 |
SUBSCRIBE_PLUGIN* |
| 217 |
find_subscribe_plugin(bool(* pred)(const SUBSCRIBE_PLUGIN*)) { |
| 218 |
gint |
| 219 |
wrapped_pred(gconstpointer sp, gconstpointer GOL_UNUSED_ARG(user_data)) { |
| 220 |
return pred((const SUBSCRIBE_PLUGIN*) sp) ? 0 : 1; |
| 221 |
} |
| 222 |
GList* elem = g_list_find_custom(subscribe_plugins, NULL, wrapped_pred); |
| 223 |
return elem ? (SUBSCRIBE_PLUGIN*) elem->data : NULL; |
| 224 |
} |
| 225 |
|
| 226 |
void |
| 227 |
foreach_display_plugin(void(* func)(DISPLAY_PLUGIN*)) { |
| 228 |
void |
| 229 |
wrapped_func(gpointer dp, gpointer GOL_UNUSED_ARG(user_data)) { |
| 230 |
func((DISPLAY_PLUGIN*) dp); |
| 231 |
} |
| 232 |
g_list_foreach(display_plugins, wrapped_func, NULL); |
| 233 |
} |
| 234 |
|
| 235 |
void |
| 236 |
foreach_subscribe_plugin(void(* func)(SUBSCRIBE_PLUGIN*)) { |
| 237 |
void |
| 238 |
wrapped_func(gpointer sp, gpointer GOL_UNUSED_ARG(user_data)) { |
| 239 |
func((SUBSCRIBE_PLUGIN*) sp); |
| 240 |
} |
| 241 |
g_list_foreach(subscribe_plugins, wrapped_func, NULL); |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
exec_sqlite3(const char tsql[], ...) { |
| 246 |
va_list list; |
| 247 |
va_start(list, tsql); |
| 248 |
|
| 249 |
char* const sql = sqlite3_vmprintf(tsql, list); |
| 250 |
gol_debug_message("request \n\t\"%s\"", sql); |
| 251 |
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) |
| 252 |
gol_debug_warning("sqlite3 reports an error.\n\t%s", sqlite3_errmsg(db)); |
| 253 |
sqlite3_free(sql); |
| 254 |
|
| 255 |
va_end(list); |
| 256 |
} |
| 257 |
|
| 258 |
static sqlite3_stmt * |
| 259 |
vprepare_sqlite3(const char* const tsql, va_list list) { |
| 260 |
char* const sql = sqlite3_vmprintf(tsql, list); |
| 261 |
gol_debug_message("request \n\t\"%s\"", sql); |
| 262 |
sqlite3_stmt* stmt = NULL; |
| 263 |
if (sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL) != SQLITE_OK) |
| 264 |
gol_debug_warning("sqlite3 reports an error.\n\t%s", sqlite3_errmsg(db)); |
| 265 |
sqlite3_free(sql); |
| 266 |
return stmt; |
| 267 |
} |
| 268 |
|
| 269 |
static sqlite3_stmt * |
| 270 |
prepare_sqlite3(const char* const tsql, ...) { |
| 271 |
va_list list; |
| 272 |
va_start(list, tsql); |
| 273 |
|
| 274 |
sqlite3_stmt* const stmt = vprepare_sqlite3(tsql, list); |
| 275 |
|
| 276 |
va_end(list); |
| 277 |
return stmt; |
| 278 |
} |
| 279 |
|
| 280 |
static void |
| 281 |
statement_sqlite3(void(* stmt_func)(sqlite3_stmt*), const char* const tsql, ...) { |
| 282 |
va_list list; |
| 283 |
va_start(list, tsql); |
| 284 |
|
| 285 |
sqlite3_stmt* const stmt = vprepare_sqlite3(tsql, list); |
| 286 |
stmt_func(stmt); |
| 287 |
|
| 288 |
sqlite3_finalize(stmt); |
| 289 |
|
| 290 |
va_end(list); |
| 291 |
} |
| 292 |
|
| 293 |
static GdkPixbuf* |
| 294 |
pixbuf_from_datadir(const gchar* filename, GError** error) { |
| 295 |
if (!filename) { |
| 296 |
g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "filename should not be NULL"); |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
gchar* const path = g_build_filename(DATADIR, "data", filename, NULL); |
| 301 |
GdkPixbuf* const pixbuf = gdk_pixbuf_new_from_file(path, error); |
| 302 |
g_free(path); |
| 303 |
|
| 304 |
return pixbuf; |
| 305 |
} |
| 306 |
|
| 307 |
static gboolean |
| 308 |
get_config_bool(const char* key, gboolean def) { |
| 309 |
gboolean ret; |
| 310 |
void |
| 311 |
get_int(sqlite3_stmt* const stmt) { |
| 312 |
ret = sqlite3_step(stmt) == SQLITE_ROW |
| 313 |
? (gboolean) sqlite3_column_int(stmt, 0) |
| 314 |
: def; |
| 315 |
} |
| 316 |
statement_sqlite3(get_int, |
| 317 |
"select value from config where key = '%q'", key); |
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
static gboolean |
| 322 |
get_subscriber_enabled(const char* name) { |
| 323 |
gboolean ret; |
| 324 |
void |
| 325 |
get_enabled(sqlite3_stmt* const stmt) { |
| 326 |
ret = sqlite3_step(stmt) == SQLITE_ROW |
| 327 |
? (gboolean) sqlite3_column_int(stmt, 0) |
| 328 |
: FALSE; |
| 329 |
} |
| 330 |
statement_sqlite3(get_enabled, |
| 331 |
"select enable from subscriber where name = '%q'", name); |
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
static gchar* |
| 336 |
get_display_parameter(const char* const name, const char* const def) { |
| 337 |
gchar* value; |
| 338 |
void |
| 339 |
get_string(sqlite3_stmt* const stmt) { |
| 340 |
value = g_strdup( |
| 341 |
sqlite3_step(stmt) == SQLITE_ROW |
| 342 |
? (char*) sqlite3_column_text(stmt, 0) |
| 343 |
: def ? def : ""); |
| 344 |
} |
| 345 |
statement_sqlite3(get_string, |
| 346 |
"select parameter from display where name = '%q'", name); |
| 347 |
return value; |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
set_display_parameter(const char* const name, const char* const value) { |
| 352 |
gol_debug_message("name: %s, parameter: %s", name, value); |
| 353 |
exec_sqlite3("delete from display where name = '%q'", name); |
| 354 |
|
| 355 |
exec_sqlite3("insert into display(name, parameter) values('%q', '%q')", name, value); |
| 356 |
} |
| 357 |
|
| 358 |
static gint |
| 359 |
get_config_value(const char* const key, const gint def) { |
| 360 |
gchar* data; |
| 361 |
gint value; |
| 362 |
void |
| 363 |
get_string(sqlite3_stmt* const stmt) { |
| 364 |
data = sqlite3_step(stmt) == SQLITE_ROW |
| 365 |
? (char*) sqlite3_column_text(stmt, 0): ""; |
| 366 |
} |
| 367 |
statement_sqlite3(get_string, |
| 368 |
"select value from config where key = '%q'", key); |
| 369 |
value = g_ascii_strtoll(data, NULL, 10); |
| 370 |
return value != 0 ? value : def; |
| 371 |
} |
| 372 |
|
| 373 |
static gchar* |
| 374 |
get_config_string(const char* const key, const char* const def) { |
| 375 |
gchar* value; |
| 376 |
void |
| 377 |
get_string(sqlite3_stmt* const stmt) { |
| 378 |
value = g_strdup( |
| 379 |
sqlite3_step(stmt) == SQLITE_ROW |
| 380 |
? (char*) sqlite3_column_text(stmt, 0) |
| 381 |
: def ? def : ""); |
| 382 |
} |
| 383 |
statement_sqlite3(get_string, |
| 384 |
"select value from config where key = '%q'", key); |
| 385 |
return value; |
| 386 |
} |
| 387 |
|
| 388 |
static void |
| 389 |
set_config_string(const char* const key, const char* const value) { |
| 390 |
exec_sqlite3("delete from config where key = '%q'", key); |
| 391 |
exec_sqlite3("insert into config(key, value) values('%q', '%q')", key, value); |
| 392 |
} |
| 393 |
|
| 394 |
static void |
| 395 |
set_config_bool(const char* key, gboolean value) { |
| 396 |
set_config_string(key, value ? "1" : "0"); |
| 397 |
} |
| 398 |
|
| 399 |
static bool |
| 400 |
get_tree_model_from_selection(gchar** const restrict pname, GtkTreeSelection* const restrict selection) { |
| 401 |
GtkTreeIter iter; |
| 402 |
GtkTreeModel* model; |
| 403 |
if (!GTK_IS_TREE_SELECTION(selection)) return FALSE; |
| 404 |
if (!gtk_tree_selection_get_selected(selection, &model, &iter)) return FALSE; |
| 405 |
|
| 406 |
gtk_tree_model_get(model, &iter, 0, pname, -1); |
| 407 |
return TRUE; |
| 408 |
} |
| 409 |
|
| 410 |
static bool |
| 411 |
get_tree_model_from_tree(gchar** const restrict pname, GtkWidget* const restrict tree) { |
| 412 |
GtkTreeSelection* const selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); |
| 413 |
return get_tree_model_from_selection(pname, selection); |
| 414 |
} |
| 415 |
|
| 416 |
static GtkTreeIter |
| 417 |
list_store_set_before_prepand(GtkListStore* const list_store, ...) { |
| 418 |
va_list list; |
| 419 |
va_start(list, list_store); |
| 420 |
GtkTreeIter iter; |
| 421 |
gtk_list_store_prepend(list_store, &iter); |
| 422 |
gtk_list_store_set_valist(list_store, &iter, list); |
| 423 |
va_end(list); |
| 424 |
return iter; |
| 425 |
} |
| 426 |
|
| 427 |
static GtkTreeIter |
| 428 |
list_store_set_after_append(GtkListStore* const list_store, ...) { |
| 429 |
va_list list; |
| 430 |
va_start(list, list_store); |
| 431 |
GtkTreeIter iter; |
| 432 |
gtk_list_store_append(list_store, &iter); |
| 433 |
gtk_list_store_set_valist(list_store, &iter, list); |
| 434 |
va_end(list); |
| 435 |
return iter; |
| 436 |
} |
| 437 |
|
| 438 |
static gpointer |
| 439 |
get_data_as_object(gpointer user_data, const gchar* const key) { |
| 440 |
return g_object_get_data(G_OBJECT(user_data), key); |
| 441 |
} |
| 442 |
|
| 443 |
static GtkComboBox* |
| 444 |
combo_box_set_active_as_object(gpointer user_data, const gchar* const key, const int index) { |
| 445 |
GtkWidget* const cbx = (GtkWidget*) get_data_as_object(user_data, key); |
| 446 |
gtk_widget_set_sensitive(cbx, TRUE); |
| 447 |
gtk_combo_box_set_active(GTK_COMBO_BOX(cbx), index); |
| 448 |
return GTK_COMBO_BOX(cbx); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
display_tree_selection_changed(GtkTreeSelection * const selection, const gpointer user_data) { |
| 453 |
gchar* name; |
| 454 |
|
| 455 |
if (!get_tree_model_from_selection(&name, selection)) return; |
| 456 |
|
| 457 |
bool |
| 458 |
is_selection_name(const DISPLAY_PLUGIN* dp) { |
| 459 |
return !g_ascii_strcasecmp(dp->name(), name); |
| 460 |
} |
| 461 |
DISPLAY_PLUGIN* const cp = find_display_plugin_or(is_selection_name, current_display); |
| 462 |
|
| 463 |
GtkWidget* const label = (GtkWidget*) get_data_as_object(user_data, "description"); |
| 464 |
gtk_label_set_markup(GTK_LABEL(label), ""); |
| 465 |
if (cp->description) { |
| 466 |
gtk_label_set_markup(GTK_LABEL(label), cp->description()); |
| 467 |
} |
| 468 |
|
| 469 |
GtkWidget* const image = (GtkWidget*) get_data_as_object(user_data, "thumbnail"); |
| 470 |
gtk_image_clear(GTK_IMAGE(image)); |
| 471 |
if (cp->thumbnail) { |
| 472 |
char ** const raw_xpm = cp->thumbnail(); |
| 473 |
if (raw_xpm) { |
| 474 |
GdkBitmap* bitmap; |
| 475 |
GdkPixmap* pixmap = gdk_pixmap_colormap_create_from_xpm_d( |
| 476 |
NULL, gdk_colormap_get_system(), &bitmap, NULL, raw_xpm); |
| 477 |
gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap); |
| 478 |
gdk_pixmap_unref(pixmap); |
| 479 |
gdk_bitmap_unref(bitmap); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
/* |
| 484 |
GtkWidget* const entry = (GtkWidget*) get_data_as_object(user_data, "parameter"); |
| 485 |
gtk_entry_set_text(GTK_ENTRY(entry), ""); |
| 486 |
gtk_widget_set_sensitive(entry, FALSE); |
| 487 |
if (cp->get_param != NULL && cp->set_param != NULL) { |
| 488 |
const gchar* param = cp->get_param(); |
| 489 |
gtk_entry_set_text(GTK_ENTRY(entry), param ? param : ""); |
| 490 |
gtk_widget_set_sensitive(entry, TRUE); |
| 491 |
} |
| 492 |
*/ |
| 493 |
g_free(name); |
| 494 |
} |
| 495 |
|
| 496 |
static void |
| 497 |
application_tree_selection_changed(GtkTreeSelection *selection, gpointer user_data) { |
| 498 |
gchar* app_name; |
| 499 |
if (!get_tree_model_from_selection(&app_name, selection)) return; |
| 500 |
|
| 501 |
GtkListStore* const model2 = (GtkListStore*) get_data_as_object(user_data, "model2"); |
| 502 |
gtk_list_store_clear(model2); |
| 503 |
|
| 504 |
void |
| 505 |
append_applications(sqlite3_stmt* const stmt) { |
| 506 |
while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 507 |
list_store_set_after_append( |
| 508 |
GTK_LIST_STORE(model2), 0, sqlite3_column_text(stmt, 0), -1); |
| 509 |
} |
| 510 |
} |
| 511 |
statement_sqlite3(append_applications, |
| 512 |
"select distinct name from application" |
| 513 |
" where app_name = '%q' order by name", app_name); |
| 514 |
|
| 515 |
g_free(app_name); |
| 516 |
|
| 517 |
gtk_widget_set_sensitive((GtkWidget*) get_data_as_object(user_data, "enable"), FALSE); |
| 518 |
gtk_widget_set_sensitive((GtkWidget*) get_data_as_object(user_data, "display"), FALSE); |
| 519 |
} |
| 520 |
|
| 521 |
static void |
| 522 |
set_as_default_clicked(GtkWidget* GOL_UNUSED_ARG(widget), gpointer user_data) { |
| 523 |
GtkTreeSelection* const selection = (GtkTreeSelection*) user_data; |
| 524 |
gchar* name; |
| 525 |
if (!get_tree_model_from_selection(&name, selection)) return; |
| 526 |
|
| 527 |
bool |
| 528 |
is_selection_name(const DISPLAY_PLUGIN* dp) { |
| 529 |
return !g_ascii_strcasecmp(dp->name(), name); |
| 530 |
} |
| 531 |
DISPLAY_PLUGIN* const dp = find_display_plugin(is_selection_name); |
| 532 |
if (dp) { |
| 533 |
current_display = dp; |
| 534 |
set_config_string("default_display", name); |
| 535 |
if (current_display->set_param) |
| 536 |
current_display->set_param(get_display_parameter(name, "")); |
| 537 |
} |
| 538 |
g_free(name); |
| 539 |
} |
| 540 |
|
| 541 |
static gboolean |
| 542 |
parameter_focus_out(GtkWidget* GOL_UNUSED_ARG(widget), GdkEvent* GOL_UNUSED_ARG(event), gpointer user_data) { |
| 543 |
GtkTreeSelection* selection = (GtkTreeSelection*) user_data; |
| 544 |
gchar* name; |
| 545 |
if (!get_tree_model_from_selection(&name, selection)) return FALSE; |
| 546 |
|
| 547 |
bool |
| 548 |
is_selection_name(const DISPLAY_PLUGIN* dp) { |
| 549 |
return !g_ascii_strcasecmp(dp->name(), name); |
| 550 |
} |
| 551 |
DISPLAY_PLUGIN* const dp = find_display_plugin(is_selection_name); |
| 552 |
if (dp) { |
| 553 |
GtkWidget* const entry = (GtkWidget*) get_data_as_object(user_data, "parameter"); |
| 554 |
const gchar* param = gtk_entry_get_text(GTK_ENTRY(entry)); |
| 555 |
dp->set_param(param); |
| 556 |
set_display_parameter(name, param); |
| 557 |
} |
| 558 |
g_free(name); |
| 559 |
return FALSE; |
| 560 |
} |
| 561 |
|
| 562 |
static void |
| 563 |
preview_clicked(GtkWidget* GOL_UNUSED_ARG(widget), gpointer user_data) { |
| 564 |
GtkTreeSelection* selection = (GtkTreeSelection*) user_data; |
| 565 |
gchar* name; |
| 566 |
if (!get_tree_model_from_selection(&name, selection)) return; |
| 567 |
|
| 568 |
bool |
| 569 |
is_selection_name(const DISPLAY_PLUGIN* dp) { |
| 570 |
return !g_ascii_strcasecmp(dp->name(), name); |
| 571 |
} |
| 572 |
DISPLAY_PLUGIN* const dp = find_display_plugin(is_selection_name); |
| 573 |
if (dp) { |
| 574 |
NOTIFICATION_INFO* const ni = g_new0(NOTIFICATION_INFO, 1); |
| 575 |
ni->title = g_strdup("Preview Display"); |
| 576 |
ni->text = g_strdup_printf( |
| 577 |
"This is a preview of the '%s' display.", dp->name()); |
| 578 |
ni->icon = g_build_filename(DATADIR, "data", "mattn.png", NULL); |
| 579 |
ni->local = TRUE; |
| 580 |
ni->timeout = get_config_value("default_timeout", 5000)/10; |
| 581 |
g_idle_add((GSourceFunc) dp->show, ni); |
| 582 |
} |
| 583 |
g_free(name); |
| 584 |
} |
| 585 |
|
| 586 |
static gboolean |
| 587 |
password_focus_out(GtkWidget* widget, GdkEvent* GOL_UNUSED_ARG(event), gpointer GOL_UNUSED_ARG(user_data)) { |
| 588 |
g_free(password); |
| 589 |
password = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget))); |
| 590 |
set_config_string("password", password); |
| 591 |
return FALSE; |
| 592 |
} |
| 593 |
|
| 594 |
static void |
| 595 |
require_password_for_local_apps_changed( |
| 596 |
GtkToggleButton *togglebutton, gpointer GOL_UNUSED_ARG(user_data)) { |
| 597 |
require_password_for_local_apps |
| 598 |
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togglebutton)); |
| 599 |
set_config_bool("require_password_for_local_apps", |
| 600 |
require_password_for_local_apps); |
| 601 |
} |
| 602 |
|
| 603 |
static void |
| 604 |
require_password_for_lan_apps_changed( |
| 605 |
GtkToggleButton *togglebutton, gpointer GOL_UNUSED_ARG(user_data)) { |
| 606 |
require_password_for_lan_apps |
| 607 |
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togglebutton)); |
| 608 |
set_config_bool("require_password_for_lan_apps", |
| 609 |
require_password_for_lan_apps); |
| 610 |
} |
| 611 |
|
| 612 |
static void |
| 613 |
subscriber_enable_toggled( |
| 614 |
GtkCellRendererToggle* GOL_UNUSED_ARG(cell), gchar* path_str, gpointer user_data) { |
| 615 |
|
| 616 |
GtkTreeModel* const model = (GtkTreeModel *) user_data; |
| 617 |
GtkTreeIter iter; |
| 618 |
GtkTreePath* const path = gtk_tree_path_new_from_string (path_str); |
| 619 |
gboolean enable; |
| 620 |
gchar* name; |
| 621 |
|
| 622 |
gtk_tree_model_get_iter (model, &iter, path); |
| 623 |
gtk_tree_model_get (model, &iter, 0, &enable, 1, &name, -1); |
| 624 |
enable = !enable; |
| 625 |
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, enable, -1); |
| 626 |
|
| 627 |
exec_sqlite3("delete from subscriber where name = '%q'", name); |
| 628 |
|
| 629 |
exec_sqlite3( |
| 630 |
"insert into subscriber(name, enable) values('%q', %d)", |
| 631 |
name, enable ? 1 : 0); |
| 632 |
|
| 633 |
bool |
| 634 |
is_model_name(const SUBSCRIBE_PLUGIN* sp) { |
| 635 |
return !g_ascii_strcasecmp(sp->name(), name); |
| 636 |
} |
| 637 |
SUBSCRIBE_PLUGIN* const sp = find_subscribe_plugin(is_model_name); |
| 638 |
if (sp) { |
| 639 |
enable ? sp->start() : sp->stop(); |
| 640 |
} |
| 641 |
|
| 642 |
g_free(name); |
| 643 |
gtk_tree_path_free(path); |
| 644 |
} |
| 645 |
|
| 646 |
static void |
| 647 |
notification_tree_selection_changed(GtkTreeSelection* GOL_UNUSED_ARG(selection), gpointer user_data) { |
| 648 |
gchar* app_name; |
| 649 |
GtkWidget* const tree1 = get_data_as_object(user_data, "tree1"); |
| 650 |
if (!get_tree_model_from_tree(&app_name, tree1)) return; |
| 651 |
|
| 652 |
gchar* name; |
| 653 |
GtkWidget* const tree2 = get_data_as_object(user_data, "tree2"); |
| 654 |
if (!get_tree_model_from_tree(&name, tree2)) return; |
| 655 |
|
| 656 |
int cbx1_index = -1; |
| 657 |
int cbx2_index = -1; |
| 658 |
|
| 659 |
void |
| 660 |
create_combo_boxes(sqlite3_stmt* const stmt) { |
| 661 |
if (sqlite3_step(stmt) != SQLITE_ROW) return; |
| 662 |
cbx1_index = sqlite3_column_int(stmt, 0) != 0 ? 0 : 1; |
| 663 |
char* const display = (char*) sqlite3_column_text(stmt, 1); |
| 664 |
bool |
| 665 |
is_display(const DISPLAY_PLUGIN* dp) { |
| 666 |
return !g_ascii_strcasecmp(dp->name(), display); |
| 667 |
} |
| 668 |
DISPLAY_PLUGIN* const dp = find_display_plugin(is_display); |
| 669 |
if (dp) { |
| 670 |
cbx2_index = g_list_index(display_plugins, dp); |
| 671 |
} |
| 672 |
} |
| 673 |
statement_sqlite3(create_combo_boxes, |
| 674 |
"select enable, display from application" |
| 675 |
" where app_name = '%q' and name = '%q'", app_name, name); |
| 676 |
|
| 677 |
combo_box_set_active_as_object(user_data, "enable", cbx1_index); |
| 678 |
combo_box_set_active_as_object(user_data, "display", cbx2_index); |
| 679 |
|
| 680 |
g_free(app_name); |
| 681 |
g_free(name); |
| 682 |
} |
| 683 |
|
| 684 |
static void |
| 685 |
notification_enable_changed(GtkComboBox *combobox, gpointer user_data) { |
| 686 |
gchar* app_name; |
| 687 |
GtkWidget* const tree1 = get_data_as_object(user_data, "tree1"); |
| 688 |
if (!get_tree_model_from_tree(&app_name, tree1)) return; |
| 689 |
|
| 690 |
gchar* name; |
| 691 |
GtkWidget* const tree2 = get_data_as_object(user_data, "tree2"); |
| 692 |
if (!get_tree_model_from_tree(&name, tree2)) { |
| 693 |
free(app_name); |
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
const gint enable = gtk_combo_box_get_active(combobox) == 0 ? 1 : 0; |
| 698 |
|
| 699 |
exec_sqlite3( |
| 700 |
"update application set enable = %d" |
| 701 |
" where app_name = '%q' and name = '%q'", |
| 702 |
enable, app_name, name); |
| 703 |
|
| 704 |
g_free(app_name); |
| 705 |
g_free(name); |
| 706 |
} |
| 707 |
|
| 708 |
static void |
| 709 |
notification_display_changed(GtkComboBox *combobox, gpointer user_data) { |
| 710 |
gchar* app_name; |
| 711 |
GtkWidget* const tree1 = get_data_as_object(user_data, "tree1"); |
| 712 |
if (!get_tree_model_from_tree(&app_name, tree1)) return; |
| 713 |
|
| 714 |
gchar* name; |
| 715 |
GtkWidget* const tree2 = get_data_as_object(user_data, "tree2"); |
| 716 |
if (!get_tree_model_from_tree(&name, tree2)) { |
| 717 |
g_free(app_name); |
| 718 |
return; |
| 719 |
} |
| 720 |
|
| 721 |
gchar* const display = gtk_combo_box_get_active_text(combobox); |
| 722 |
|
| 723 |
exec_sqlite3( |
| 724 |
"update application set display = '%q'" |
| 725 |
" where app_name = '%q' and name = '%q'", |
| 726 |
display, app_name, name); |
| 727 |
|
| 728 |
g_free(display); |
| 729 |
g_free(app_name); |
| 730 |
g_free(name); |
| 731 |
} |
| 732 |
|
| 733 |
static void |
| 734 |
append_new_menu_item_from_stock( |
| 735 |
GtkMenuShell* const menu, const gchar* const stock, GCallback callback) { |
| 736 |
GtkWidget* const menu_item = gtk_image_menu_item_new_from_stock(stock, NULL); |
| 737 |
g_signal_connect(G_OBJECT(menu_item), "activate", callback, NULL); |
| 738 |
gtk_menu_shell_append(menu, menu_item); |
| 739 |
} |
| 740 |
|
| 741 |
static void |
| 742 |
application_delete(GtkWidget* GOL_UNUSED_ARG(widget), gpointer user_data) { |
| 743 |
GtkTreeIter iter1; |
| 744 |
GtkWidget* const tree1 = get_data_as_object(user_data, "tree1"); |
| 745 |
GtkTreeSelection* const selection1 |
| 746 |
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree1)); |
| 747 |
GtkTreeModel* model1; |
| 748 |
if (!gtk_tree_selection_get_selected(selection1, &model1, &iter1)) return; |
| 749 |
|
| 750 |
gchar* app_name; |
| 751 |
gtk_tree_model_get(model1, &iter1, 0, &app_name, -1); |
| 752 |
|
| 753 |
GtkTreeIter iter2; |
| 754 |
GtkWidget* const tree2 = get_data_as_object(user_data, "tree2"); |
| 755 |
GtkTreeSelection* const selection2 |
| 756 |
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree2)); |
| 757 |
GtkTreeModel* model2; |
| 758 |
if (gtk_tree_selection_get_selected(selection2, &model2, &iter2)) { |
| 759 |
gchar* name; |
| 760 |
gtk_tree_model_get(model2, &iter2, 0, &name, -1); |
| 761 |
exec_sqlite3( |
| 762 |
"delete from application where app_name = '%q' and name = '%q'", |
| 763 |
app_name, name); |
| 764 |
g_free(name); |
| 765 |
|
| 766 |
gtk_list_store_remove(GTK_LIST_STORE(model2), &iter2); |
| 767 |
} else { |
| 768 |
exec_sqlite3("delete from application where app_name = '%q'", app_name); |
| 769 |
gtk_list_store_remove(GTK_LIST_STORE(model1), &iter1); |
| 770 |
gtk_list_store_clear(GTK_LIST_STORE(model2)); |
| 771 |
} |
| 772 |
g_free(app_name); |
| 773 |
|
| 774 |
combo_box_set_active_as_object(user_data, "enable", -1); |
| 775 |
combo_box_set_active_as_object(user_data, "display", -1); |
| 776 |
} |
| 777 |
|
| 778 |
static gboolean |
| 779 |
tree_view_button_pressed(GtkWidget* widget, GdkEventButton* event, gpointer user_data) { |
| 780 |
if (event->type != GDK_BUTTON_PRESS || event->button != 3) return FALSE; |
| 781 |
|
| 782 |
GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget)); |
| 783 |
GtkTreeIter iter; |
| 784 |
GtkTreeModel* model; |
| 785 |
if (!gtk_tree_selection_get_selected(selection, &model, &iter)) return FALSE; |
| 786 |
|
| 787 |
GtkTreePath* path; |
| 788 |
gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), |
| 789 |
(gint) event->x, (gint) event->y, &path, NULL, NULL, NULL); |
| 790 |
gchar* name; |
| 791 |
gtk_tree_model_get(model, &iter, 0, &name, -1); |
| 792 |
|
| 793 |
GtkWidget* contextmenu = GTK_WIDGET(user_data); |
| 794 |
gtk_widget_show_all(contextmenu); |
| 795 |
gtk_menu_popup(GTK_MENU(contextmenu), NULL, NULL, NULL, NULL, |
| 796 |
event ? event->button : 0, gdk_event_get_time((GdkEvent*) event)); |
| 797 |
return TRUE; |
| 798 |
} |
| 799 |
|
| 800 |
typedef gboolean button_pressed_callback_t(GtkWidget*, GdkEventButton*, gpointer); |
| 801 |
typedef void changed_callback_t(GtkTreeSelection*, gpointer); |
| 802 |
|
| 803 |
static GtkListStore* |
| 804 |
vertical_list_new(const char* const model_name, const char* const tree_name, |
| 805 |
button_pressed_callback_t* const button_pressed_callback, |
| 806 |
GtkWidget* const contextmenu, changed_callback_t* const changed_callback, |
| 807 |
const GtkWidget* const hbox, const char* const column_attribute_name) { |
| 808 |
GtkListStore* const model = |
| 809 |
(GtkListStore *) gtk_list_store_new(1, G_TYPE_STRING); |
| 810 |
g_object_set_data(G_OBJECT(setting_dialog), model_name, model); |
| 811 |
|
| 812 |
GtkWidget* const tree_view = |
| 813 |
gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
| 814 |
g_object_set_data(G_OBJECT(setting_dialog), tree_name, tree_view); |
| 815 |
g_signal_connect(G_OBJECT(tree_view), "button-press-event", |
| 816 |
G_CALLBACK(button_pressed_callback), contextmenu); |
| 817 |
|
| 818 |
GtkTreeSelection* const select = |
| 819 |
gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)); |
| 820 |
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE); |
| 821 |
g_signal_connect(G_OBJECT(select), "changed", |
| 822 |
G_CALLBACK(changed_callback), setting_dialog); |
| 823 |
|
| 824 |
gtk_box_pack_start(GTK_BOX(hbox), tree_view, FALSE, FALSE, 0); |
| 825 |
|
| 826 |
GtkTreeViewColumn* const column = |
| 827 |
gtk_tree_view_column_new_with_attributes( |
| 828 |
column_attribute_name, gtk_cell_renderer_text_new(), "text", 0, NULL); |
| 829 |
gtk_tree_view_column_set_min_width(GTK_TREE_VIEW_COLUMN(column), 80); |
| 830 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column); |
| 831 |
|
| 832 |
return model; |
| 833 |
} |
| 834 |
|
| 835 |
static void |
| 836 |
settings_clicked(GtkWidget* GOL_UNUSED_ARG(widget), GdkEvent* GOL_UNUSED_ARG(event), gpointer GOL_UNUSED_ARG(user_data)) { |
| 837 |
if (setting_dialog) { |
| 838 |
gtk_window_present(GTK_WINDOW(setting_dialog)); |
| 839 |
return; |
| 840 |
} |
| 841 |
setting_dialog = gtk_dialog_new_with_buttons( |
| 842 |
"Settings", NULL, GTK_DIALOG_MODAL, |
| 843 |
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL); |
| 844 |
gchar* const path = g_build_filename(DATADIR, "data", "icon256.png", NULL); |
| 845 |
gtk_window_set_icon_from_file(GTK_WINDOW(setting_dialog), path, NULL); |
| 846 |
g_free(path); |
| 847 |
gtk_window_set_position(GTK_WINDOW(setting_dialog), GTK_WIN_POS_CENTER); |
| 848 |
|
| 849 |
GtkWidget* notebook; |
| 850 |
GtkWidget* contextmenu; |
| 851 |
|
| 852 |
notebook = gtk_notebook_new(); |
| 853 |
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(setting_dialog)->vbox), notebook); |
| 854 |
|
| 855 |
{ |
| 856 |
GtkWidget* hbox = gtk_hbox_new(FALSE, 5); |
| 857 |
gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
| 858 |
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), |
| 859 |
hbox, gtk_label_new("Display")); |
| 860 |
|
| 861 |
GtkListStore* model = |
| 862 |
(GtkListStore *) gtk_list_store_new(1, G_TYPE_STRING); |
| 863 |
GtkWidget* tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
| 864 |
GtkTreeSelection* select = gtk_tree_view_get_selection( |
| 865 |
GTK_TREE_VIEW(tree_view)); |
| 866 |
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE); |
| 867 |
g_signal_connect(G_OBJECT(select), "changed", |
| 868 |
G_CALLBACK(display_tree_selection_changed), setting_dialog); |
| 869 |
gtk_box_pack_start(GTK_BOX(hbox), tree_view, FALSE, FALSE, 0); |
| 870 |
GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( |
| 871 |
"Display", gtk_cell_renderer_text_new(), "text", 0, NULL); |
| 872 |
gtk_tree_view_column_set_min_width(GTK_TREE_VIEW_COLUMN(column), 80); |
| 873 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column); |
| 874 |
|
| 875 |
GtkWidget* vbox = gtk_vbox_new(FALSE, 20); |
| 876 |
gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, FALSE, 0); |
| 877 |
GtkWidget* label = gtk_label_new(""); |
| 878 |
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); |
| 879 |
gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_CHAR); |
| 880 |
g_object_set_data(G_OBJECT(setting_dialog), "description", label); |
| 881 |
GtkWidget* align = gtk_alignment_new(0, 0, 0, 0); |
| 882 |
gtk_container_add(GTK_CONTAINER(align), label); |
| 883 |
gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, FALSE, 0); |
| 884 |
GtkWidget* image = gtk_image_new(); |
| 885 |
g_object_set_data(G_OBJECT(setting_dialog), "thumbnail", image); |
| 886 |
gtk_box_pack_start(GTK_BOX(vbox), image, TRUE, TRUE, 0); |
| 887 |
|
| 888 |
hbox = gtk_hbox_new(FALSE, 5); |
| 889 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0); |
| 890 |
|
| 891 |
GtkWidget* button = gtk_button_new_with_label("Set as Default"); |
| 892 |
g_signal_connect(G_OBJECT(button), "clicked", |
| 893 |
G_CALLBACK(set_as_default_clicked), select); |
| 894 |
gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0); |
| 895 |
button = gtk_button_new_with_label("Preview"); |
| 896 |
g_signal_connect(G_OBJECT(button), "clicked", |
| 897 |
G_CALLBACK(preview_clicked), select); |
| 898 |
gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0); |
| 899 |
|
| 900 |
hbox = gtk_hbox_new(FALSE, 5); |
| 901 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0); |
| 902 |
|
| 903 |
/* |
| 904 |
GtkWidget* entry = gtk_entry_new(); |
| 905 |
g_signal_connect(G_OBJECT(entry), "focus-out-event", |
| 906 |
G_CALLBACK(parameter_focus_out), NULL); |
| 907 |
g_object_set_data(G_OBJECT(setting_dialog), "parameter", entry); |
| 908 |
gtk_box_pack_end(GTK_BOX(hbox), entry, FALSE, FALSE, 0); |
| 909 |
label = gtk_label_new("Parameter:"); |
| 910 |
gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0); |
| 911 |
*/ |
| 912 |
|
| 913 |
void |
| 914 |
append_display_plugins(DISPLAY_PLUGIN* dp) { |
| 915 |
GtkTreeIter iter = list_store_set_after_append( |
| 916 |
GTK_LIST_STORE(model), 0, dp->name(), -1); |
| 917 |
if (dp == current_display) |
| 918 |
gtk_tree_selection_select_iter(select, &iter); |
| 919 |
} |
| 920 |
foreach_display_plugin(append_display_plugins); |
| 921 |
} |
| 922 |
|
| 923 |
{ |
| 924 |
contextmenu = gtk_menu_new(); |
| 925 |
GtkWidget* const menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_DELETE, NULL); |
| 926 |
g_signal_connect(G_OBJECT(menu_item), "activate", |
| 927 |
G_CALLBACK(application_delete), setting_dialog); |
| 928 |
gtk_menu_shell_append(GTK_MENU_SHELL(contextmenu), menu_item); |
| 929 |
|
| 930 |
GtkWidget* const hbox = gtk_hbox_new(FALSE, 5); |
| 931 |
gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
| 932 |
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), |
| 933 |
hbox, gtk_label_new("Application")); |
| 934 |
|
| 935 |
GtkListStore* const model1 = |
| 936 |
vertical_list_new("model1", "tree1", |
| 937 |
tree_view_button_pressed, contextmenu, |
| 938 |
application_tree_selection_changed, hbox, "Application"); |
| 939 |
|
| 940 |
vertical_list_new("model2", "tree2", |
| 941 |
tree_view_button_pressed, contextmenu, |
| 942 |
notification_tree_selection_changed, hbox, "Notification"); |
| 943 |
|
| 944 |
GtkWidget* const frame = gtk_frame_new("Setting"); |
| 945 |
gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0); |
| 946 |
|
| 947 |
GtkWidget* const vbox = gtk_vbox_new(FALSE, 5); |
| 948 |
gtk_container_set_border_width(GTK_CONTAINER(vbox), 10); |
| 949 |
gtk_container_add(GTK_CONTAINER(frame), vbox); |
| 950 |
|
| 951 |
{ |
| 952 |
GtkWidget* const hbox = gtk_hbox_new(FALSE, 5); |
| 953 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
| 954 |
|
| 955 |
GtkWidget* const label = gtk_label_new("Enable:"); |
| 956 |
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); |
| 957 |
GtkWidget* const combobox = gtk_combo_box_new_text(); |
| 958 |
g_object_set_data(G_OBJECT(setting_dialog), "enable", combobox); |
| 959 |
gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), "Enable"); |
| 960 |
gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), "Disable"); |
| 961 |
gtk_widget_set_sensitive(combobox, FALSE); |
| 962 |
g_signal_connect(G_OBJECT(combobox), "changed", |
| 963 |
G_CALLBACK(notification_enable_changed), setting_dialog); |
| 964 |
gtk_box_pack_start(GTK_BOX(hbox), combobox, FALSE, FALSE, 0); |
| 965 |
} |
| 966 |
|
| 967 |
{ |
| 968 |
GtkWidget* const hbox = gtk_hbox_new(FALSE, 5); |
| 969 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
| 970 |
|
| 971 |
GtkWidget* const label = gtk_label_new("Display:"); |
| 972 |
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); |
| 973 |
GtkWidget* const combobox = gtk_combo_box_new_text(); |
| 974 |
g_object_set_data(G_OBJECT(setting_dialog), "display", combobox); |
| 975 |
gtk_widget_set_sensitive(combobox, FALSE); |
| 976 |
g_signal_connect(G_OBJECT(combobox), "changed", |
| 977 |
G_CALLBACK(notification_display_changed), setting_dialog); |
| 978 |
gtk_box_pack_start(GTK_BOX(hbox), combobox, FALSE, FALSE, 0); |
| 979 |
|
| 980 |
{ |
| 981 |
char* const sql = "select distinct app_name from application order by app_name"; |
| 982 |
sqlite3_stmt *stmt = NULL; |
| 983 |
sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL); |
| 984 |
while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 985 |
list_store_set_after_append( |
| 986 |
GTK_LIST_STORE(model1), 0, sqlite3_column_text(stmt, 0), -1); |
| 987 |
} |
| 988 |
sqlite3_finalize(stmt); |
| 989 |
} |
| 990 |
|
| 991 |
void |
| 992 |
append_display_plugins(DISPLAY_PLUGIN* dp) { |
| 993 |
gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), dp->name()); |
| 994 |
} |
| 995 |
foreach_display_plugin(append_display_plugins); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
{ |
| 1000 |
GtkWidget* vbox = gtk_vbox_new(FALSE, 5); |
| 1001 |
gtk_container_set_border_width(GTK_CONTAINER(vbox), 10); |
| 1002 |
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, |
| 1003 |
gtk_label_new("Security")); |
| 1004 |
|
| 1005 |
GtkWidget* checkbutton; |
| 1006 |
checkbutton = gtk_check_button_new_with_label( |
| 1007 |
"Require password for local apps"); |
| 1008 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), |
| 1009 |
require_password_for_local_apps); |
| 1010 |
g_signal_connect(G_OBJECT(checkbutton), "toggled", |
| 1011 |
G_CALLBACK(require_password_for_local_apps_changed), NULL); |
| 1012 |
gtk_box_pack_start(GTK_BOX(vbox), checkbutton, FALSE, FALSE, 0); |
| 1013 |
checkbutton = gtk_check_button_new_with_label( |
| 1014 |
"Require password for LAN apps"); |
| 1015 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), |
| 1016 |
require_password_for_lan_apps); |
| 1017 |
g_signal_connect(G_OBJECT(checkbutton), "toggled", |
| 1018 |
G_CALLBACK(require_password_for_lan_apps_changed), NULL); |
| 1019 |
gtk_box_pack_start(GTK_BOX(vbox), checkbutton, FALSE, FALSE, 0); |
| 1020 |
GtkWidget* hbox = gtk_hbox_new(FALSE, 5); |
| 1021 |
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
| 1022 |
GtkWidget* label = gtk_label_new("Password:"); |
| 1023 |
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); |
| 1024 |
GtkWidget* entry = gtk_entry_new(); |
| 1025 |
gtk_entry_set_text(GTK_ENTRY(entry), password); |
| 1026 |
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); |
| 1027 |
g_signal_connect(G_OBJECT(entry), "focus-out-event", |
| 1028 |
G_CALLBACK(password_focus_out), NULL); |
| 1029 |
gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 0); |
| 1030 |
} |
| 1031 |
|
| 1032 |
{ |
| 1033 |
GtkWidget* hbox = gtk_hbox_new(FALSE, 5); |
| 1034 |
gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
| 1035 |
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), |
| 1036 |
hbox, gtk_label_new("Subscribe")); |
| 1037 |
|
| 1038 |
GtkListStore* model = (GtkListStore *) gtk_list_store_new( |
| 1039 |
3, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING); |
| 1040 |
GtkWidget* tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
| 1041 |
GtkTreeSelection* select = gtk_tree_view_get_selection( |
| 1042 |
GTK_TREE_VIEW(tree_view)); |
| 1043 |
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE); |
| 1044 |
gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, TRUE, 0); |
| 1045 |
|
| 1046 |
GtkCellRenderer* renderer = gtk_cell_renderer_toggle_new(); |
| 1047 |
g_signal_connect(renderer, "toggled", |
| 1048 |
G_CALLBACK(subscriber_enable_toggled), model); |
| 1049 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), |
| 1050 |
gtk_tree_view_column_new_with_attributes( |
| 1051 |
"Enable", renderer, "active", 0, NULL)); |
| 1052 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), |
| 1053 |
gtk_tree_view_column_new_with_attributes( |
| 1054 |
"Name", gtk_cell_renderer_text_new(), "text", 1, NULL)); |
| 1055 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), |
| 1056 |
gtk_tree_view_column_new_with_attributes( |
| 1057 |
"Description", gtk_cell_renderer_text_new(), "markup", 2, NULL)); |
| 1058 |
|
| 1059 |
void |
| 1060 |
append_subscribe_plugins(SUBSCRIBE_PLUGIN* sp) { |
| 1061 |
list_store_set_after_append(GTK_LIST_STORE(model), |
| 1062 |
0, get_subscriber_enabled(sp->name()), |
| 1063 |
1, sp->name(), |
| 1064 |
2, sp->description(), |
| 1065 |
-1); |
| 1066 |
} |
| 1067 |
foreach_subscribe_plugin(append_subscribe_plugins); |
| 1068 |
} |
| 1069 |
|
| 1070 |
{ |
| 1071 |
GtkWidget* hbox = gtk_hbox_new(FALSE, 5); |
| 1072 |
gtk_container_set_border_width(GTK_CONTAINER(hbox), 10); |
| 1073 |
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), |
| 1074 |
hbox, gtk_label_new("Notifications")); |
| 1075 |
|
| 1076 |
GtkListStore* model = (GtkListStore *) gtk_list_store_new( |
| 1077 |
3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); |
| 1078 |
g_object_set_data(G_OBJECT(setting_dialog), "notifications", model); |
| 1079 |
GtkWidget* tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model)); |
| 1080 |
|
| 1081 |
GtkTreeSelection* select = gtk_tree_view_get_selection( |
| 1082 |
GTK_TREE_VIEW(tree_view)); |
| 1083 |
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE); |
| 1084 |
GtkWidget* swin = gtk_scrolled_window_new(NULL, NULL); |
| 1085 |
gtk_container_add(GTK_CONTAINER(swin), tree_view); |
| 1086 |
gtk_box_pack_start(GTK_BOX(hbox), swin, TRUE, TRUE, 0); |
| 1087 |
|
| 1088 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), |
| 1089 |
gtk_tree_view_column_new_with_attributes( |
| 1090 |
"Datetime", gtk_cell_renderer_text_new(), "text", 0, NULL)); |
| 1091 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), |
| 1092 |
gtk_tree_view_column_new_with_attributes( |
| 1093 |
"Title", gtk_cell_renderer_text_new(), "text", 1, NULL)); |
| 1094 |
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), |
| 1095 |
gtk_tree_view_column_new_with_attributes( |
| 1096 |
"Text", gtk_cell_renderer_text_new(), "text", 2, NULL)); |
| 1097 |
|
| 1098 |
{ |
| 1099 |
char* const sql = "select received, title, text from notification order by received desc"; |
| 1100 |
sqlite3_stmt *stmt = NULL; |
| 1101 |
sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL); |
| 1102 |
while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 1103 |
list_store_set_after_append( |
| 1104 |
GTK_LIST_STORE(model), |
| 1105 |
0, sqlite3_column_text(stmt, 0), |
| 1106 |
1, sqlite3_column_text(stmt, 1), |
| 1107 |
2, sqlite3_column_text(stmt, 2), |
| 1108 |
-1); |
| 1109 |
} |
| 1110 |
sqlite3_finalize(stmt); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
gtk_widget_set_size_request(setting_dialog, 500, 500); |
| 1115 |
gtk_widget_show_all(setting_dialog); |
| 1116 |
gtk_dialog_run(GTK_DIALOG(setting_dialog)); |
| 1117 |
gtk_widget_destroy(setting_dialog); |
| 1118 |
gtk_widget_destroy(contextmenu); |
| 1119 |
setting_dialog = NULL; |
| 1120 |
} |
| 1121 |
|
| 1122 |
static void |
| 1123 |
about_click(GtkWidget* GOL_UNUSED_ARG(widget), gpointer GOL_UNUSED_ARG(user_data)) { |
| 1124 |
if (about_dialog) { |
| 1125 |
gtk_window_present(GTK_WINDOW(about_dialog)); |
| 1126 |
return; |
| 1127 |
} |
| 1128 |
|
| 1129 |
const gchar* authors[] = { |
| 1130 |
"Yasuhiro Matsumoto <[email protected]>", |
| 1131 |
"Kohei Takahashi <[email protected]>", |
| 1132 |
NULL |
| 1133 |
}; |
| 1134 |
gchar* contents = NULL; |
| 1135 |
|
| 1136 |
about_dialog = gtk_about_dialog_new(); |
| 1137 |
gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(about_dialog), "Growl For Linux"); |
| 1138 |
gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about_dialog), |
| 1139 |
"https://github.com/mattn/growl-for-linux/"); |
| 1140 |
gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(about_dialog), |
| 1141 |
"A notification system for linux"); |
| 1142 |
gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(about_dialog), authors); |
| 1143 |
gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about_dialog), PACKAGE_VERSION); |
| 1144 |
if (g_file_get_contents("COPYING", &contents, NULL, NULL)) { |
| 1145 |
gchar* utf8 = g_locale_to_utf8(contents, -1, NULL, NULL, NULL); |
| 1146 |
g_free(contents); |
| 1147 |
contents = NULL; |
| 1148 |
gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(about_dialog), utf8); |
| 1149 |
g_free(utf8); |
| 1150 |
} |
| 1151 |
gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about_dialog), |
| 1152 |
"http://mattn.kaoriya.net/"); |
| 1153 |
{ |
| 1154 |
GdkPixbuf* logo = pixbuf_from_datadir("icon256.png", NULL); |
| 1155 |
gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(about_dialog), logo); |
| 1156 |
g_object_unref(G_OBJECT(logo)); |
| 1157 |
} |
| 1158 |
gtk_window_set_position(GTK_WINDOW(about_dialog), GTK_WIN_POS_CENTER); |
| 1159 |
gtk_dialog_run(GTK_DIALOG(about_dialog)); |
| 1160 |
gtk_widget_destroy(about_dialog); |
| 1161 |
about_dialog = NULL; |
| 1162 |
} |
| 1163 |
|
| 1164 |
static void |
| 1165 |
exit_clicked(GtkWidget* GOL_UNUSED_ARG(widget), GdkEvent* GOL_UNUSED_ARG(event), gpointer GOL_UNUSED_ARG(user_data)) { |
| 1166 |
gtk_main_quit(); |
| 1167 |
} |
| 1168 |
|
| 1169 |
static char* |
| 1170 |
crlf_to_term_or_null(char* str) { |
| 1171 |
str = strstr(str, "\r\n"); |
| 1172 |
if (str) { |
| 1173 |
memcpy(str, "\0", 2); |
| 1174 |
str += 2; |
| 1175 |
} |
| 1176 |
return str; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static char* |
| 1180 |
crlf_to_term_and_skip(char* const str) { |
| 1181 |
char* tmp = crlf_to_term_or_null(str); |
| 1182 |
if (!tmp) { |
| 1183 |
tmp = str + strlen(str); |
| 1184 |
} |
| 1185 |
return tmp; |
| 1186 |
} |
| 1187 |
|
| 1188 |
static char* |
| 1189 |
cr_to_lf(char* const str) { |
| 1190 |
for (char* itr = str; (itr = strchr(itr, '\r')); *itr++ = '\n'); |
| 1191 |
return str; |
| 1192 |
} |
| 1193 |
|
| 1194 |
static void |
| 1195 |
str_swap(char ** restrict _left, char ** restrict _right) { |
| 1196 |
char *tmp = *_left; |
| 1197 |
*_left = *_right; |
| 1198 |
*_right = tmp; |
| 1199 |
} |
| 1200 |
|
| 1201 |
typedef struct { |
| 1202 |
const int sock; |
| 1203 |
const char* command; |
| 1204 |
const char* application_name; |
| 1205 |
const char* notification_name; |
| 1206 |
const char* notification_display_name; |
| 1207 |
} CLIENT_INFO; |
| 1208 |
|
| 1209 |
static bool |
| 1210 |
raise_notification(const CLIENT_INFO ci, NOTIFICATION_INFO* const ni) { |
| 1211 |
const bool valid = ni && ni->title && ni->text; |
| 1212 |
|
| 1213 |
gchar* const cmd_result = valid |
| 1214 |
? g_strdup_printf(GNTP_OK_STRING_LITERAL("1.0", "%s"), ci.command) |
| 1215 |
: g_strdup(GNTP_ERROR_STRING_LITERAL("1.0", "Invalid data", "Invalid data")); |
| 1216 |
|
| 1217 |
if (cmd_result) { |
| 1218 |
send(ci.sock, cmd_result, strlen(cmd_result), 0); |
| 1219 |
g_free(cmd_result); |
| 1220 |
} else { |
| 1221 |
g_critical("g_strdup or g_strdup_printf failed."); |
| 1222 |
return false; |
| 1223 |
} |
| 1224 |
if (!valid) return false; |
| 1225 |
|
| 1226 |
if (gol_status == GOL_STATUS_DND) |
| 1227 |
return true; |
| 1228 |
|
| 1229 |
DISPLAY_PLUGIN* cp = NULL; |
| 1230 |
// Received name. |
| 1231 |
if (ci.notification_display_name) { |
| 1232 |
bool |
| 1233 |
is_ndn(const DISPLAY_PLUGIN* dp) { |
| 1234 |
return !g_ascii_strcasecmp(dp->name(), ci.notification_display_name); |
| 1235 |
} |
| 1236 |
cp = find_display_plugin(is_ndn); |
| 1237 |
} |
| 1238 |
|
| 1239 |
// Lookup application default notification name. |
| 1240 |
if (!cp) { |
| 1241 |
sqlite3_stmt* const stmt = prepare_sqlite3( |
| 1242 |
"select enable, display from application" |
| 1243 |
" where app_name = '%q' and name = '%q'", |
| 1244 |
ci.application_name, ci.notification_name); |
| 1245 |
if (sqlite3_step(stmt) == SQLITE_ROW && sqlite3_column_int(stmt, 0)) { |
| 1246 |
const char* const adn = (const char*) sqlite3_column_text(stmt, 1); |
| 1247 |
bool |
| 1248 |
is_adn(const DISPLAY_PLUGIN* dp) { |
| 1249 |
return !g_ascii_strcasecmp(dp->name(), adn); |
| 1250 |
} |
| 1251 |
cp = find_display_plugin(is_adn); |
| 1252 |
} |
| 1253 |
sqlite3_finalize(stmt); |
| 1254 |
} |
| 1255 |
|
| 1256 |
// Lookup system default notification name. |
| 1257 |
if (!cp) { |
| 1258 |
const char* const sdn = get_config_string("default_display", "Fog"); |
| 1259 |
bool |
| 1260 |
is_sdn(const DISPLAY_PLUGIN* dp) { |
| 1261 |
return !g_ascii_strcasecmp(dp->name(), sdn); |
| 1262 |
} |
| 1263 |
cp = find_display_plugin_or(is_sdn, current_display); |
| 1264 |
} |
| 1265 |
|
| 1266 |
ni->timeout = get_config_value("default_timeout", 5000)/10; |
| 1267 |
g_idle_add((GSourceFunc) cp->show, ni); // call once |
| 1268 |
return true; |
| 1269 |
} |
| 1270 |
|
| 1271 |
static void |
| 1272 |
parse_identifiers(char* ptr) { |
| 1273 |
const gchar* const confdir = (const gchar*) g_get_user_config_dir(); |
| 1274 |
gchar* const resourcedir = g_build_path(G_DIR_SEPARATOR_S, confdir, "gol", "resource", NULL); |
| 1275 |
if (!g_file_test(resourcedir, G_FILE_TEST_IS_DIR)) |
| 1276 |
g_mkdir_with_parents(resourcedir, 0700); |
| 1277 |
while (*ptr) { |
| 1278 |
char* identifier = NULL; |
| 1279 |
long length = 0; |
| 1280 |
while (*ptr) { |
| 1281 |
char* const line = ptr; |
| 1282 |
ptr = crlf_to_term_and_skip(ptr); |
| 1283 |
if (*line == '\0') break; |
| 1284 |
cr_to_lf(line); |
| 1285 |
|
| 1286 |
const char* const colon = strchr(line, ':'); |
| 1287 |
if (colon) { |
| 1288 |
char* value = g_strdup(skipsp(colon + 1)); |
| 1289 |
if (!strncmp(line, "Identifier:", 11)) |
| 1290 |
identifier = g_strdup(value); |
| 1291 |
if (identifier && !strncmp(line, "Length:", 7)) |
| 1292 |
length = atol(value); |
| 1293 |
g_free(value); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
if (identifier) { |
| 1297 |
gchar* const filename = g_build_filename(resourcedir, identifier, NULL); |
| 1298 |
if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) |
| 1299 |
g_file_set_contents(filename, ptr, length, NULL); |
| 1300 |
g_free(filename); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
g_free(resourcedir); |
| 1304 |
} |
| 1305 |
|
| 1306 |
static gpointer |
| 1307 |
gntp_recv_proc(gpointer user_data) { |
| 1308 |
int sock = (int)(intptr_t) user_data; |
| 1309 |
bool is_local_app = FALSE; |
| 1310 |
|
| 1311 |
struct sockaddr_in client; |
| 1312 |
socklen_t client_len = sizeof(client); |
| 1313 |
memset(&client, 0, sizeof(client)); |
| 1314 |
if (!getsockname(sock, (struct sockaddr *) &client, &client_len)) { |
| 1315 |
const char* addr = inet_ntoa(((struct sockaddr_in *)(void*)&client)->sin_addr); |
| 1316 |
if (addr && !strcmp(addr, "127.0.0.1")) { |
| 1317 |
is_local_app = TRUE; |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
char* ptr = ""; |
| 1322 |
const size_t r = read_all(sock, &ptr); |
| 1323 |
char* const top = ptr; |
| 1324 |
|
| 1325 |
if (!strncmp(ptr, "GNTP/1.0 ", 9)) { |
| 1326 |
ptr += 9; |
| 1327 |
|
| 1328 |
const char* const command = ptr; |
| 1329 |
if (!strncmp(command, "REGISTER ", 9)) ptr += 8; |
| 1330 |
else if (!strncmp(command, "NOTIFY ", 7)) ptr += 6; |
| 1331 |
else goto leave; |
| 1332 |
|
| 1333 |
*ptr++ = 0; |
| 1334 |
|
| 1335 |
char* data = NULL; |
| 1336 |
if (!strncmp(ptr, "NONE", 4) && strchr("\r\n ", *(ptr+5))) { |
| 1337 |
if (is_local_app && require_password_for_local_apps) goto leave; |
| 1338 |
if (!is_local_app && require_password_for_lan_apps) goto leave; |
| 1339 |
if (!(ptr = crlf_to_term_or_null(ptr))) goto leave; |
| 1340 |
const size_t datalen = r - (ptr-top) - 4; |
| 1341 |
data = (char*) malloc(datalen+1); |
| 1342 |
if (!data) goto leave; |
| 1343 |
memcpy(data, ptr, datalen); |
| 1344 |
data[datalen] = '\0'; |
| 1345 |
} else { |
| 1346 |
if (strncmp(ptr, "NONE ", 5) && |
| 1347 |
strncmp(ptr, "AES:", 4) && |
| 1348 |
strncmp(ptr, "DES:", 4) && |
| 1349 |
strncmp(ptr, "3DES:", 5)) goto leave; |
| 1350 |
|
| 1351 |
const char* const crypt_algorythm = ptr; |
| 1352 |
if (!(ptr = strpbrk(ptr, ": "))) goto leave; |
| 1353 |
*ptr++ = 0; |
| 1354 |
|
| 1355 |
char* const iv = ptr; |
| 1356 |
if (strcmp(crypt_algorythm, "NONE")) { |
| 1357 |
if (!(ptr = strchr(ptr, ' '))) goto leave; |
| 1358 |
*ptr++ = 0; |
| 1359 |
} |
| 1360 |
ptr = (char*) skipsp(ptr); |
| 1361 |
|
| 1362 |
if (strncmp(ptr, "MD5:", 4) && |
| 1363 |
strncmp(ptr, "SHA1:", 5) && |
| 1364 |
strncmp(ptr, "SHA256:", 7)) goto leave; |
| 1365 |
|
| 1366 |
const char* const hash_algorythm = ptr; |
| 1367 |
if (!(ptr = strchr(ptr, ':'))) goto leave; |
| 1368 |
*ptr++ = 0; |
| 1369 |
|
| 1370 |
char* const key = ptr; |
| 1371 |
if (!(ptr = strchr(ptr, '.'))) goto leave; |
| 1372 |
*ptr++ = 0; |
| 1373 |
|
| 1374 |
char* const salt = ptr; |
| 1375 |
|
| 1376 |
if (!(ptr = crlf_to_term_or_null(ptr))) goto leave; |
| 1377 |
|
| 1378 |
const size_t saltlen = strlen(salt) / 2; |
| 1379 |
for (size_t n = 0; n < saltlen; n++) |
| 1380 |
salt[n] = unhex(salt[n * 2]) * 16 + unhex(salt[n * 2 + 1]); |
| 1381 |
const size_t keylen = strlen(key) / 2; |
| 1382 |
for (size_t n = 0; n < keylen; n++) |
| 1383 |
key[n] = unhex(key[n * 2]) * 16 + unhex(key[n * 2 + 1]); |
| 1384 |
const size_t ivlen = strlen(iv) / 2; |
| 1385 |
for (size_t n = 0; n < ivlen; n++) |
| 1386 |
iv[n] = unhex(iv[n * 2]) * 16 + unhex(iv[n * 2 + 1]); |
| 1387 |
|
| 1388 |
unsigned char digest[32] = {0}; |
| 1389 |
if (!strcmp(hash_algorythm, "MD5")) { |
| 1390 |
MD5_CTX ctx; |
| 1391 |
MD5_Init(&ctx); |
| 1392 |
MD5_Update(&ctx, password, strlen(password)); |
| 1393 |
MD5_Update(&ctx, salt, saltlen); |
| 1394 |
MD5_Final(digest, &ctx); |
| 1395 |
} |
| 1396 |
else if (!strcmp(hash_algorythm, "SHA1")) { |
| 1397 |
SHA_CTX ctx; |
| 1398 |
SHA1_Init(&ctx); |
| 1399 |
SHA1_Update(&ctx, password, strlen(password)); |
| 1400 |
SHA1_Update(&ctx, salt, saltlen); |
| 1401 |
SHA1_Final(digest, &ctx); |
| 1402 |
} |
| 1403 |
else if (!strcmp(hash_algorythm, "SHA256")) { |
| 1404 |
SHA256_CTX ctx; |
| 1405 |
SHA256_Init(&ctx); |
| 1406 |
SHA256_Update(&ctx, password, strlen(password)); |
| 1407 |
SHA256_Update(&ctx, salt, saltlen); |
| 1408 |
SHA256_Final(digest, &ctx); |
| 1409 |
} |
| 1410 |
|
| 1411 |
data = (char*) calloc(r, 1); |
| 1412 |
if (!data) goto leave; |
| 1413 |
if (!strcmp(crypt_algorythm, "AES")) { |
| 1414 |
AES_KEY aeskey; |
| 1415 |
AES_set_decrypt_key(digest, 24 * 8, &aeskey); |
| 1416 |
AES_cbc_encrypt((unsigned char*) ptr, (unsigned char*) data, |
| 1417 |
r-(ptr-top)-6, &aeskey, (unsigned char*) iv, AES_DECRYPT); |
| 1418 |
} |
| 1419 |
else if (!strcmp(crypt_algorythm, "DES")) { |
| 1420 |
DES_key_schedule schedule; |
| 1421 |
DES_set_key_unchecked((const_DES_cblock*) &digest, &schedule); |
| 1422 |
DES_ncbc_encrypt((unsigned char*) ptr, (unsigned char*) data, |
| 1423 |
r-(ptr-top)-6, &schedule, (const_DES_cblock*) &iv, DES_DECRYPT); |
| 1424 |
} |
| 1425 |
else if (!strcmp(crypt_algorythm, "3DES")) { |
| 1426 |
DES_key_schedule schedule1, schedule2, schedule3; |
| 1427 |
DES_set_key_unchecked((const_DES_cblock*) (digest+ 0), &schedule1); |
| 1428 |
DES_set_key_unchecked((const_DES_cblock*) (digest+ 8), &schedule2); |
| 1429 |
DES_set_key_unchecked((const_DES_cblock*) (digest+16), &schedule3); |
| 1430 |
DES_ede3_cbc_encrypt((unsigned char*) ptr, (unsigned char*) data, |
| 1431 |
r-(ptr-top)-6, &schedule1, &schedule2, &schedule3, |
| 1432 |
(const_DES_cblock*) &iv, DES_DECRYPT); |
| 1433 |
} else { |
| 1434 |
data = strdup(ptr); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
ptr = data; |
| 1439 |
if (!strcmp(command, "REGISTER")) { |
| 1440 |
char* application_name = NULL; |
| 1441 |
char* application_icon = NULL; |
| 1442 |
long notifications_count = 0; |
| 1443 |
while (*ptr) { |
| 1444 |
char* const line = ptr; |
| 1445 |
ptr = crlf_to_term_and_skip(ptr); |
| 1446 |
if (*line== '\0') break; |
| 1447 |
cr_to_lf(line); |
| 1448 |
|
| 1449 |
const char* const colon = strchr(line, ':'); |
| 1450 |
if (colon) { |
| 1451 |
char* value = g_strdup(skipsp(colon + 1)); |
| 1452 |
if (!strncmp(line, "Application-Name:", 17)) { |
| 1453 |
str_swap(&value, &application_name); |
| 1454 |
} |
| 1455 |
else if (!strncmp(line, "Application-Icon:", 17)) { |
| 1456 |
str_swap(&value, &application_icon); |
| 1457 |
} |
| 1458 |
else if (!strncmp(line, "Notifications-Count:", 20)) { |
| 1459 |
notifications_count = atol(value); |
| 1460 |
} |
| 1461 |
g_free(value); |
| 1462 |
} |
| 1463 |
} |
| 1464 |
int n; |
| 1465 |
for (n = 0; n < notifications_count; n++) { |
| 1466 |
char* notification_name = NULL; |
| 1467 |
char* notification_icon = NULL; |
| 1468 |
gboolean notification_enabled = FALSE; |
| 1469 |
gboolean notification_sticky = FALSE; |
| 1470 |
char* notification_display_name = NULL; |
| 1471 |
while (*ptr) { |
| 1472 |
char* const line = ptr; |
| 1473 |
ptr = crlf_to_term_and_skip(ptr); |
| 1474 |
if (*line== '\0') break; |
| 1475 |
cr_to_lf(line); |
| 1476 |
|
| 1477 |
const char* const colon = strchr(line, ':'); |
| 1478 |
if (colon) { |
| 1479 |
char* value = g_strdup(skipsp(colon + 1)); |
| 1480 |
if (!strncmp(line, "Notification-Name:", 18)) { |
| 1481 |
str_swap(&value, ¬ification_name); |
| 1482 |
} |
| 1483 |
else if (!strncmp(line, "Notification-Icon:", 18)) { |
| 1484 |
str_swap(&value, ¬ification_icon); |
| 1485 |
} |
| 1486 |
else if (!strncmp(line, "Notification-Enabled:", 21)) { |
| 1487 |
notification_enabled = strcasecmp(value, "true") == 0; |
| 1488 |
} |
| 1489 |
else if (!strncmp(line, "Notification-Display-Name:", 26)) { |
| 1490 |
str_swap(&value, ¬ification_display_name); |
| 1491 |
} |
| 1492 |
else if (!strncmp(line, "Notification-Sticky:", 20)) { |
| 1493 |
notification_sticky = strcasecmp(value, "true") == 0; |
| 1494 |
} |
| 1495 |
g_free(value); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
int exist = 0; |
| 1500 |
void |
| 1501 |
get_count(sqlite3_stmt* const stmt) { |
| 1502 |
exist = sqlite3_step(stmt) == SQLITE_ROW |
| 1503 |
? (gboolean) sqlite3_column_int(stmt, 0) |
| 1504 |
: 0; |
| 1505 |
} |
| 1506 |
statement_sqlite3(get_count, |
| 1507 |
"select count(*) from application where app_name = '%q' and name = '%q'", |
| 1508 |
application_name, notification_name); |
| 1509 |
|
| 1510 |
if (!exist) { |
| 1511 |
exec_sqlite3( |
| 1512 |
"delete from application where app_name = '%q' and name = '%q'", |
| 1513 |
application_name, notification_name); |
| 1514 |
|
| 1515 |
exec_sqlite3( |
| 1516 |
"insert into application(" |
| 1517 |
"app_name, app_icon, name, icon, enable, display, sticky)" |
| 1518 |
" values('%q', '%q', '%q', '%q', %d, '%q', %d)", |
| 1519 |
application_name, |
| 1520 |
application_icon ? application_icon : "", |
| 1521 |
notification_name, |
| 1522 |
notification_icon ? notification_icon : "", |
| 1523 |
notification_enabled, |
| 1524 |
notification_display_name ? |
| 1525 |
notification_display_name : "Fog", |
| 1526 |
notification_sticky); |
| 1527 |
} |
| 1528 |
|
| 1529 |
g_free(notification_name); |
| 1530 |
g_free(notification_icon); |
| 1531 |
g_free(notification_display_name); |
| 1532 |
} |
| 1533 |
parse_identifiers(ptr); |
| 1534 |
|
| 1535 |
ptr = n == notifications_count |
| 1536 |
? GNTP_OK_STRING_LITERAL("1.0", "REGISTER") |
| 1537 |
: GNTP_ERROR_STRING_LITERAL("1.0", "Invalid data", "Invalid data"); |
| 1538 |
send(sock, ptr, strlen(ptr), 0); |
| 1539 |
g_free(application_name); |
| 1540 |
g_free(application_icon); |
| 1541 |
} else { |
| 1542 |
NOTIFICATION_INFO* ni = g_new0(NOTIFICATION_INFO, 1); |
| 1543 |
if (!ni) { |
| 1544 |
perror("g_new0"); |
| 1545 |
goto leave; |
| 1546 |
} |
| 1547 |
char* application_name = NULL; |
| 1548 |
char* notification_name = NULL; |
| 1549 |
char* notification_display_name = NULL; |
| 1550 |
while (*ptr) { |
| 1551 |
char* const line = ptr; |
| 1552 |
ptr = crlf_to_term_and_skip(ptr); |
| 1553 |
if (*line == '\0') break; |
| 1554 |
cr_to_lf(line); |
| 1555 |
|
| 1556 |
const char* const colon = strchr(line, ':'); |
| 1557 |
if (colon) { |
| 1558 |
char* value = g_strdup(skipsp(colon + 1)); |
| 1559 |
if (!strncmp(line, "Application-Name:", 17)) { |
| 1560 |
str_swap(&value, &application_name); |
| 1561 |
} |
| 1562 |
else if (!strncmp(line, "Notification-Name:", 18)) { |
| 1563 |
str_swap(&value, ¬ification_name); |
| 1564 |
} |
| 1565 |
else if (!strncmp(line, "Notification-Title:", 19)) { |
| 1566 |
str_swap(&value, &ni->title); |
| 1567 |
} |
| 1568 |
else if (!strncmp(line, "Notification-Text:", 18)) { |
| 1569 |
str_swap(&value, &ni->text); |
| 1570 |
} |
| 1571 |
else if (!strncmp(line, "Notification-Icon:", 18)) { |
| 1572 |
str_swap(&value, &ni->icon); |
| 1573 |
} |
| 1574 |
else if (!strncmp(line, "Notification-Sticky:", 20)) { |
| 1575 |
ni->sticky = strcasecmp(value, "true") == 0; |
| 1576 |
} |
| 1577 |
else if (!strncmp(line, "Notification-Callback-Target:", 29)) { |
| 1578 |
str_swap(&value, &ni->url); |
| 1579 |
} |
| 1580 |
else if (!strncmp(line, "Notification-Display-Name:", 26)) { |
| 1581 |
str_swap(&value, ¬ification_display_name); |
| 1582 |
} |
| 1583 |
g_free(value); |
| 1584 |
} |
| 1585 |
} |
| 1586 |
parse_identifiers(ptr); |
| 1587 |
|
| 1588 |
exec_sqlite3( |
| 1589 |
"insert into notification(" |
| 1590 |
"title, text, icon, url, received)" |
| 1591 |
" values('%q', '%q', '%q', '%q', current_timestamp)", |
| 1592 |
ni->title, ni->text, |
| 1593 |
ni->icon ? ni->icon : "", |
| 1594 |
ni->url ? ni->url : ""); |
| 1595 |
if (setting_dialog) { |
| 1596 |
GtkTreeModel* const model |
| 1597 |
= (GtkTreeModel*) get_data_as_object(setting_dialog, "notifications"); |
| 1598 |
gchar* value; |
| 1599 |
void |
| 1600 |
get_current_timestamp(sqlite3_stmt* const stmt) { |
| 1601 |
value = g_strdup( |
| 1602 |
sqlite3_step(stmt) == SQLITE_ROW |
| 1603 |
? (char*) sqlite3_column_text(stmt, 0) |
| 1604 |
: ""); |
| 1605 |
} |
| 1606 |
statement_sqlite3(get_current_timestamp, "select current_timestamp"); |
| 1607 |
list_store_set_before_prepand(GTK_LIST_STORE(model), |
| 1608 |
0, value, |
| 1609 |
1, ni->title, |
| 1610 |
2, ni->text, |
| 1611 |
-1); |
| 1612 |
g_free(value); |
| 1613 |
} |
| 1614 |
|
| 1615 |
raise_notification( |
| 1616 |
(CLIENT_INFO){ |
| 1617 |
.sock = sock, |
| 1618 |
.command = command, |
| 1619 |
.application_name = application_name, |
| 1620 |
.notification_name = notification_name, |
| 1621 |
.notification_display_name = notification_display_name, |
| 1622 |
}, ni); |
| 1623 |
|
| 1624 |
g_free(notification_name); |
| 1625 |
g_free(notification_display_name); |
| 1626 |
g_free(application_name); |
| 1627 |
} |
| 1628 |
free(data); |
| 1629 |
} else { |
| 1630 |
ptr = GNTP_ERROR_STRING_LITERAL("1.0", "Invalid command", "Invalid command"); |
| 1631 |
send(sock, ptr, strlen(ptr), 0); |
| 1632 |
} |
| 1633 |
free(top); |
| 1634 |
shutdown(sock, SD_BOTH); |
| 1635 |
closesocket(sock); |
| 1636 |
return NULL; |
| 1637 |
|
| 1638 |
leave: |
| 1639 |
ptr = GNTP_ERROR_STRING_LITERAL("1.0", "Invalid request", "Invalid request"); |
| 1640 |
send(sock, ptr, strlen(ptr), 0); |
| 1641 |
free(top); |
| 1642 |
shutdown(sock, SD_BOTH); |
| 1643 |
closesocket(sock); |
| 1644 |
return NULL; |
| 1645 |
} |
| 1646 |
|
| 1647 |
#ifdef _WIN32 |
| 1648 |
static BOOL WINAPI |
| 1649 |
ctrl_handler(DWORD type) { |
| 1650 |
if (about_dialog) gtk_dialog_response(GTK_DIALOG(about_dialog), GTK_RESPONSE_CLOSE); |
| 1651 |
if (setting_dialog) gtk_dialog_response(GTK_DIALOG(setting_dialog), GTK_RESPONSE_CLOSE); |
| 1652 |
|
| 1653 |
SetConsoleCtrlHandler(ctrl_handler, TRUE); |
| 1654 |
gtk_main_quit(); |
| 1655 |
return TRUE; |
| 1656 |
} |
| 1657 |
#else |
| 1658 |
static void |
| 1659 |
signal_handler(int num) { |
| 1660 |
if (about_dialog) gtk_dialog_response(GTK_DIALOG(about_dialog), GTK_RESPONSE_CLOSE); |
| 1661 |
if (setting_dialog) gtk_dialog_response(GTK_DIALOG(setting_dialog), GTK_RESPONSE_CLOSE); |
| 1662 |
|
| 1663 |
signal(num, signal_handler); |
| 1664 |
gtk_main_quit(); |
| 1665 |
} |
| 1666 |
#endif |
| 1667 |
|
| 1668 |
/* |
| 1669 |
static GdkPixbuf* |
| 1670 |
disabled_pixbuf(GdkPixbuf *pixbuf) { |
| 1671 |
GdkPixbuf* gray = gdk_pixbuf_copy(pixbuf); |
| 1672 |
int rowstride = gdk_pixbuf_get_rowstride(gray); |
| 1673 |
int pixstride = gdk_pixbuf_get_has_alpha(gray) ? 4 : 3; |
| 1674 |
guchar* pixels = gdk_pixbuf_get_pixels(gray); |
| 1675 |
int n_rows = gdk_pixbuf_get_height(gray); |
| 1676 |
int width = gdk_pixbuf_get_width(gray); |
| 1677 |
int row = 0; |
| 1678 |
|
| 1679 |
while (row < n_rows) { |
| 1680 |
guchar *p = pixels + row * rowstride; |
| 1681 |
guchar *end = p + (pixstride * width); |
| 1682 |
while (p != end) { |
| 1683 |
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11) |
| 1684 |
double v = INTENSITY(p[0], p[1], p[2]); |
| 1685 |
#undef INTENSITY |
| 1686 |
p[0] = (guchar) v; |
| 1687 |
p[1] = (guchar) v; |
| 1688 |
p[2] = (guchar) v; |
| 1689 |
p += pixstride; |
| 1690 |
} |
| 1691 |
row++; |
| 1692 |
} |
| 1693 |
return gray; |
| 1694 |
} |
| 1695 |
*/ |
| 1696 |
|
| 1697 |
#ifdef HAVE_APP_INDICATOR |
| 1698 |
static void |
| 1699 |
gol_status_toggle(GtkMenuItem* menu_item, gpointer GOL_UNUSED_ARG(user_data)) { |
| 1700 |
switch (gol_status) |
| 1701 |
{ |
| 1702 |
case GOL_STATUS_NORMAL: |
| 1703 |
gol_status = GOL_STATUS_DND; |
| 1704 |
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE); |
| 1705 |
gtk_menu_item_set_label(menu_item, "Switch to on"); |
| 1706 |
break; |
| 1707 |
case GOL_STATUS_DND: |
| 1708 |
gol_status = GOL_STATUS_NORMAL; |
| 1709 |
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ATTENTION); |
| 1710 |
gtk_menu_item_set_label(menu_item, "Switch to off"); |
| 1711 |
break; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
static void |
| 1715 |
create_menu() { |
| 1716 |
gchar* theme_path; |
| 1717 |
GtkWidget* menu_item; |
| 1718 |
|
| 1719 |
indicator = app_indicator_new("Growl", "icon_dnd", |
| 1720 |
APP_INDICATOR_CATEGORY_APPLICATION_STATUS); |
| 1721 |
theme_path = g_build_filename(DATADIR, "data", NULL); |
| 1722 |
app_indicator_set_icon_theme_path(indicator, theme_path); |
| 1723 |
g_free(theme_path); |
| 1724 |
|
| 1725 |
popup_menu = gtk_menu_new(); |
| 1726 |
menu_item = gtk_menu_item_new_with_label("Switch to off"); |
| 1727 |
gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), menu_item); |
| 1728 |
gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), |
| 1729 |
gtk_separator_menu_item_new()); |
| 1730 |
append_new_menu_item_from_stock(GTK_MENU_SHELL(popup_menu), |
| 1731 |
GTK_STOCK_PREFERENCES, G_CALLBACK(settings_clicked)); |
| 1732 |
append_new_menu_item_from_stock(GTK_MENU_SHELL(popup_menu), |
| 1733 |
GTK_STOCK_ABOUT, G_CALLBACK(about_click)); |
| 1734 |
gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), |
| 1735 |
gtk_separator_menu_item_new()); |
| 1736 |
append_new_menu_item_from_stock(GTK_MENU_SHELL(popup_menu), |
| 1737 |
GTK_STOCK_QUIT, G_CALLBACK(exit_clicked)); |
| 1738 |
app_indicator_set_menu(indicator, GTK_MENU(popup_menu)); |
| 1739 |
|
| 1740 |
g_signal_connect(G_OBJECT(menu_item), "activate", |
| 1741 |
G_CALLBACK(gol_status_toggle), NULL); |
| 1742 |
|
| 1743 |
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ATTENTION); |
| 1744 |
app_indicator_set_icon_full(indicator, "icon_dnd", NULL); |
| 1745 |
app_indicator_set_attention_icon_full(indicator, "icon", NULL); |
| 1746 |
gtk_widget_show_all(popup_menu); |
| 1747 |
} |
| 1748 |
#else |
| 1749 |
static gboolean |
| 1750 |
gol_status_toggle(GtkStatusIcon* status_icon, GdkEvent* event, gpointer user_data) { |
| 1751 |
switch (gol_status) |
| 1752 |
{ |
| 1753 |
case GOL_STATUS_NORMAL: |
| 1754 |
gol_status = GOL_STATUS_DND; |
| 1755 |
gtk_status_icon_set_from_pixbuf(status_icon, status_icon_dnd_pixbuf); |
| 1756 |
break; |
| 1757 |
case GOL_STATUS_DND: |
| 1758 |
gol_status = GOL_STATUS_NORMAL; |
| 1759 |
gtk_status_icon_set_from_pixbuf(status_icon, status_icon_pixbuf); |
| 1760 |
break; |
| 1761 |
} |
| 1762 |
return FALSE; |
| 1763 |
} |
| 1764 |
|
| 1765 |
static void |
| 1766 |
my_gtk_status_icon_position_menu( |
| 1767 |
GtkMenu* const menu, gint* const x, gint* const y, |
| 1768 |
gboolean* const push_in, const gpointer user_data) { |
| 1769 |
gtk_status_icon_position_menu(menu, x, y, push_in, user_data); |
| 1770 |
#ifdef _WIN32 |
| 1771 |
RECT rect; |
| 1772 |
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0); |
| 1773 |
const gint h = GTK_WIDGET(menu)->requisition.height; |
| 1774 |
if (*y + h > rect.bottom) *y -= h; |
| 1775 |
#endif |
| 1776 |
} |
| 1777 |
|
| 1778 |
static void |
| 1779 |
status_icon_popup( |
| 1780 |
GtkStatusIcon* const status_icon, const guint button, |
| 1781 |
const guint32 activate_time, const gpointer menu) { |
| 1782 |
gtk_menu_popup(GTK_MENU(menu), NULL, NULL, |
| 1783 |
my_gtk_status_icon_position_menu, status_icon, button, activate_time); |
| 1784 |
} |
| 1785 |
|
| 1786 |
static void |
| 1787 |
create_menu() { |
| 1788 |
status_icon = gtk_status_icon_new(); |
| 1789 |
gtk_status_icon_set_tooltip(status_icon, "Growl"); |
| 1790 |
|
| 1791 |
// TODO: absolute path |
| 1792 |
status_icon_pixbuf = pixbuf_from_datadir("icon.png", NULL); |
| 1793 |
status_icon_dnd_pixbuf = pixbuf_from_datadir("icon_dnd.png", NULL); |
| 1794 |
|
| 1795 |
gtk_status_icon_set_from_pixbuf(status_icon, status_icon_pixbuf); |
| 1796 |
|
| 1797 |
popup_menu = gtk_menu_new(); |
| 1798 |
g_signal_connect(G_OBJECT(status_icon), "popup-menu", |
| 1799 |
G_CALLBACK(status_icon_popup), popup_menu); |
| 1800 |
|
| 1801 |
g_signal_connect(G_OBJECT(status_icon), "button-release-event", |
| 1802 |
G_CALLBACK(gol_status_toggle), 0); |
| 1803 |
|
| 1804 |
append_new_menu_item_from_stock(GTK_MENU_SHELL(popup_menu), |
| 1805 |
GTK_STOCK_PREFERENCES, G_CALLBACK(settings_clicked)); |
| 1806 |
append_new_menu_item_from_stock(GTK_MENU_SHELL(popup_menu), |
| 1807 |
GTK_STOCK_ABOUT, G_CALLBACK(about_click)); |
| 1808 |
gtk_menu_shell_append(GTK_MENU_SHELL(popup_menu), |
| 1809 |
gtk_separator_menu_item_new()); |
| 1810 |
append_new_menu_item_from_stock(GTK_MENU_SHELL(popup_menu), |
| 1811 |
GTK_STOCK_QUIT, G_CALLBACK(exit_clicked)); |
| 1812 |
|
| 1813 |
gtk_widget_show_all(popup_menu); |
| 1814 |
gtk_status_icon_set_visible(status_icon, TRUE); |
| 1815 |
} |
| 1816 |
#endif |
| 1817 |
|
| 1818 |
static void |
| 1819 |
destroy_menu() { |
| 1820 |
if (popup_menu) { |
| 1821 |
gtk_widget_destroy(popup_menu); |
| 1822 |
} |
| 1823 |
#ifndef HAVE_APP_INDICATOR |
| 1824 |
if (status_icon) { |
| 1825 |
gtk_status_icon_set_visible(GTK_STATUS_ICON(status_icon), FALSE); |
| 1826 |
g_object_unref(G_OBJECT(status_icon)); |
| 1827 |
} |
| 1828 |
#endif |
| 1829 |
if (status_icon_pixbuf) { |
| 1830 |
g_object_unref(G_OBJECT(status_icon_pixbuf)); |
| 1831 |
} |
| 1832 |
if (status_icon_dnd_pixbuf) { |
| 1833 |
g_object_unref(G_OBJECT(status_icon_dnd_pixbuf)); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
static gboolean |
| 1838 |
load_config() { |
| 1839 |
const gchar* const confdir = (const gchar*) g_get_user_config_dir(); |
| 1840 |
gchar* const appdir = g_build_path(G_DIR_SEPARATOR_S, confdir, "gol", NULL); |
| 1841 |
if (g_mkdir_with_parents(appdir, 0700) < 0) { |
| 1842 |
perror("mkdir"); |
| 1843 |
g_critical("Can't create directory: %s", appdir); |
| 1844 |
g_free(appdir); |
| 1845 |
return FALSE; |
| 1846 |
} |
| 1847 |
gchar* const confdb = g_build_filename(appdir, "config.db", NULL); |
| 1848 |
g_free(appdir); |
| 1849 |
const gboolean exist = g_file_test(confdb, G_FILE_TEST_EXISTS); |
| 1850 |
if (sqlite3_open(confdb, &db) != SQLITE_OK) { |
| 1851 |
g_critical("Can't open database: %s", confdb); |
| 1852 |
g_free(confdb); |
| 1853 |
return FALSE; |
| 1854 |
} |
| 1855 |
g_free(confdb); |
| 1856 |
|
| 1857 |
if (!exist) { |
| 1858 |
if (sqlite3_exec(db, "create table config" |
| 1859 |
"(key text not null primary key, value text not null)", |
| 1860 |
NULL, NULL, NULL) != SQLITE_OK) { |
| 1861 |
g_critical("Can't create configuration table"); |
| 1862 |
return FALSE; |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
gchar* const version = get_config_string("version", ""); |
| 1867 |
if (strcmp(version, PACKAGE_VERSION)) { |
| 1868 |
const char* sqls[] = { |
| 1869 |
"drop table _notification", |
| 1870 |
"drop table _application", |
| 1871 |
"drop table _subscriber", |
| 1872 |
"drop table _display", |
| 1873 |
"alter table application rename to _application", |
| 1874 |
"alter table subscriber rename to _subscriber", |
| 1875 |
"alter table display rename to _display", |
| 1876 |
"create table notification(" |
| 1877 |
"title text not null," |
| 1878 |
"text text not null," |
| 1879 |
"icon text," |
| 1880 |
"url text," |
| 1881 |
"received timestamp not null)", |
| 1882 |
"create table application(" |
| 1883 |
"app_name text not null," |
| 1884 |
"app_icon text not null," |
| 1885 |
"name text not null," |
| 1886 |
"icon text not null," |
| 1887 |
"enable int not null," |
| 1888 |
"display text not null," |
| 1889 |
"sticky int not null," |
| 1890 |
"primary key(app_name, name))", |
| 1891 |
"create table subscriber(" |
| 1892 |
"name text not null primary key," |
| 1893 |
"enable int not null)", |
| 1894 |
"create table display(" |
| 1895 |
"name text not null primary key," |
| 1896 |
"parameter text)", |
| 1897 |
"insert into notification from select * from _notification", |
| 1898 |
"insert into application from select * from _application", |
| 1899 |
"insert into subscriber from select * from _subscriber", |
| 1900 |
"insert into display from select * from _display", |
| 1901 |
"drop table _notification", |
| 1902 |
"drop table _application", |
| 1903 |
"drop table _subscriber", |
| 1904 |
"drop table _display", |
| 1905 |
NULL |
| 1906 |
}; |
| 1907 |
for (const char* const* sql = sqls; *sql; ++sql) { |
| 1908 |
sqlite3_exec(db, *sql, NULL, NULL, NULL); |
| 1909 |
} |
| 1910 |
set_config_string("version", PACKAGE_VERSION); |
| 1911 |
} |
| 1912 |
g_free(version); |
| 1913 |
|
| 1914 |
password = get_config_string("password", ""); |
| 1915 |
require_password_for_local_apps = |
| 1916 |
get_config_bool("require_password_for_local_apps", FALSE); |
| 1917 |
require_password_for_lan_apps = |
| 1918 |
get_config_bool("require_password_for_lan_apps", FALSE); |
| 1919 |
|
| 1920 |
return TRUE; |
| 1921 |
} |
| 1922 |
|
| 1923 |
static void |
| 1924 |
unload_config() { |
| 1925 |
g_free(password); |
| 1926 |
if (db) sqlite3_close(db); |
| 1927 |
} |
| 1928 |
|
| 1929 |
static void |
| 1930 |
unload_display_plugins() { |
| 1931 |
void |
| 1932 |
close_plugin(DISPLAY_PLUGIN* dp) { |
| 1933 |
if (dp->term) dp->term(); |
| 1934 |
g_module_close(dp->handle); |
| 1935 |
g_free(dp); |
| 1936 |
} |
| 1937 |
foreach_display_plugin(close_plugin); |
| 1938 |
g_list_free(display_plugins); |
| 1939 |
display_plugins = NULL; |
| 1940 |
} |
| 1941 |
|
| 1942 |
static gboolean |
| 1943 |
load_display_plugins() { |
| 1944 |
gchar* const path = g_build_filename(LIBDIR, "display", NULL); |
| 1945 |
GDir* const dir = g_dir_open(path, 0, NULL); |
| 1946 |
if (!dir) { |
| 1947 |
perror("open"); |
| 1948 |
g_critical("Display plugin directory isn't found: %s", path); |
| 1949 |
return FALSE; |
| 1950 |
} |
| 1951 |
|
| 1952 |
gchar* const default_display = get_config_string("default_display", "Fog"); |
| 1953 |
|
| 1954 |
current_display = NULL; |
| 1955 |
const gchar *filename; |
| 1956 |
while ((filename = g_dir_read_name(dir))) { |
| 1957 |
if (!g_str_has_suffix(filename, G_MODULE_SUFFIX)) |
| 1958 |
continue; |
| 1959 |
|
| 1960 |
gchar* const filepath = g_build_filename(path, filename, NULL); |
| 1961 |
GModule* const handle = g_module_open(filepath, G_MODULE_BIND_LAZY); |
| 1962 |
g_free(filepath); |
| 1963 |
if (!handle) { |
| 1964 |
continue; |
| 1965 |
} |
| 1966 |
DISPLAY_PLUGIN* const dp = g_new0(DISPLAY_PLUGIN, 1); |
| 1967 |
if (!dp) { |
| 1968 |
g_critical("fatal: g_new0(DISPLAY_PLUGIN, 1) failed"); |
| 1969 |
g_module_close(handle); |
| 1970 |
unload_display_plugins(); |
| 1971 |
break; |
| 1972 |
} |
| 1973 |
dp->handle = handle; |
| 1974 |
g_module_symbol(handle, "display_show", (void**) &dp->show); |
| 1975 |
g_module_symbol(handle, "display_init", (void**) &dp->init); |
| 1976 |
g_module_symbol(handle, "display_term", (void**) &dp->term); |
| 1977 |
g_module_symbol(handle, "display_name", (void**) &dp->name); |
| 1978 |
g_module_symbol(handle, "display_description", (void**) &dp->description); |
| 1979 |
g_module_symbol(handle, "display_thumbnail", (void**) &dp->thumbnail); |
| 1980 |
g_module_symbol(handle, "display_set_param", (void**) &dp->set_param); |
| 1981 |
g_module_symbol(handle, "display_get_param", (void**) &dp->get_param); |
| 1982 |
const char* const name = dp->name ? dp->name() : NULL; |
| 1983 |
if (name && dp->init && !dp->init()) { |
| 1984 |
g_module_close(dp->handle); |
| 1985 |
g_free(dp); |
| 1986 |
continue; |
| 1987 |
} |
| 1988 |
|
| 1989 |
display_plugins = g_list_append(display_plugins, dp); |
| 1990 |
gol_debug_message("load display plugins \"%s\".", name); |
| 1991 |
|
| 1992 |
char* const sql = sqlite3_mprintf("select name, value from display where name = '%q'", name); |
| 1993 |
sqlite3_stmt *stmt = NULL; |
| 1994 |
sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL); |
| 1995 |
if (sqlite3_column_text(stmt, 0)) { |
| 1996 |
if (dp->set_param) dp->set_param((const char*) sqlite3_column_text(stmt, 1)); |
| 1997 |
} |
| 1998 |
sqlite3_finalize(stmt); |
| 1999 |
|
| 2000 |
if (!g_ascii_strcasecmp(name, default_display)) |
| 2001 |
current_display = dp; |
| 2002 |
} |
| 2003 |
|
| 2004 |
g_dir_close(dir); |
| 2005 |
g_free(path); |
| 2006 |
g_free(default_display); |
| 2007 |
|
| 2008 |
if (g_list_length(display_plugins) == 0) { |
| 2009 |
g_critical("No display plugins found."); |
| 2010 |
return FALSE; |
| 2011 |
} |
| 2012 |
|
| 2013 |
if (!current_display) current_display = g_list_nth_data(display_plugins, 0); |
| 2014 |
if (current_display->set_param) |
| 2015 |
current_display->set_param(get_display_parameter(current_display->name(), "")); |
| 2016 |
|
| 2017 |
return TRUE; |
| 2018 |
} |
| 2019 |
|
| 2020 |
static void |
| 2021 |
subscribe_show(NOTIFICATION_INFO* const ni) { |
| 2022 |
ni->timeout = get_config_value("default_timeout", 5000)/10; |
| 2023 |
g_idle_add((GSourceFunc) current_display->show, ni); |
| 2024 |
} |
| 2025 |
|
| 2026 |
static void |
| 2027 |
unload_subscribe_plugins() { |
| 2028 |
void |
| 2029 |
close_plugin(SUBSCRIBE_PLUGIN* sp) { |
| 2030 |
if (sp->term) sp->term(); |
| 2031 |
g_module_close(sp->handle); |
| 2032 |
g_free(sp); |
| 2033 |
} |
| 2034 |
foreach_subscribe_plugin(close_plugin); |
| 2035 |
g_list_free(subscribe_plugins); |
| 2036 |
subscribe_plugins = NULL; |
| 2037 |
} |
| 2038 |
|
| 2039 |
static gboolean |
| 2040 |
load_subscribe_plugins() { |
| 2041 |
gchar* const path = g_build_filename(LIBDIR, "subscribe", NULL); |
| 2042 |
GDir* const dir = g_dir_open(path, 0, NULL); |
| 2043 |
if (!dir) { |
| 2044 |
g_warning("Subscribe plugin directory isn't found: %s", path); |
| 2045 |
return TRUE; |
| 2046 |
} |
| 2047 |
|
| 2048 |
sc.show = subscribe_show; |
| 2049 |
const gchar *filename; |
| 2050 |
while ((filename = g_dir_read_name(dir))) { |
| 2051 |
if (!g_str_has_suffix(filename, G_MODULE_SUFFIX)) |
| 2052 |
continue; |
| 2053 |
|
| 2054 |
gchar* const filepath = g_build_filename(path, filename, NULL); |
| 2055 |
GModule* const handle = g_module_open(filepath, G_MODULE_BIND_LAZY); |
| 2056 |
g_free(filepath); |
| 2057 |
if (!handle) continue; |
| 2058 |
SUBSCRIBE_PLUGIN* const sp = g_new0(SUBSCRIBE_PLUGIN, 1); |
| 2059 |
if (!sp) { |
| 2060 |
g_critical("fatal: g_new0(SUBSCRIBE_PLUGIN, 1) failed"); |
| 2061 |
g_module_close(handle); |
| 2062 |
unload_subscribe_plugins(); |
| 2063 |
break; |
| 2064 |
} |
| 2065 |
sp->handle = handle; |
| 2066 |
g_module_symbol(handle, "subscribe_start", (void**) &sp->start); |
| 2067 |
g_module_symbol(handle, "subscribe_stop", (void**) &sp->stop); |
| 2068 |
g_module_symbol(handle, "subscribe_init", (void**) &sp->init); |
| 2069 |
g_module_symbol(handle, "subscribe_term", (void**) &sp->term); |
| 2070 |
g_module_symbol(handle, "subscribe_name", (void**) &sp->name); |
| 2071 |
g_module_symbol(handle, "subscribe_description", (void**) &sp->description); |
| 2072 |
g_module_symbol(handle, "subscribe_thumbnail", (void**) &sp->thumbnail); |
| 2073 |
const char* const name = sp->name ? sp->name() : NULL; |
| 2074 |
if (name && sp->init && !sp->init(&sc)) { |
| 2075 |
g_module_close(sp->handle); |
| 2076 |
g_free(sp); |
| 2077 |
continue; |
| 2078 |
} |
| 2079 |
|
| 2080 |
subscribe_plugins = g_list_append(subscribe_plugins, sp); |
| 2081 |
gol_debug_message("load subscriber plugins \"%s\".", name); |
| 2082 |
if (get_subscriber_enabled(name)) sp->start(); |
| 2083 |
} |
| 2084 |
|
| 2085 |
g_dir_close(dir); |
| 2086 |
g_free(path); |
| 2087 |
|
| 2088 |
return TRUE; |
| 2089 |
} |
| 2090 |
|
| 2091 |
static gboolean |
| 2092 |
gntp_accepted(GIOChannel* const source, GIOCondition GOL_UNUSED_ARG(condition), gpointer GOL_UNUSED_ARG(user_data)) { |
| 2093 |
int fd = g_io_channel_unix_get_fd(source); |
| 2094 |
int sock; |
| 2095 |
struct sockaddr_in client; |
| 2096 |
socklen_t client_len = sizeof(client); |
| 2097 |
memset(&client, 0, sizeof(client)); |
| 2098 |
if ((sock = accept(fd, (struct sockaddr *) &client, &client_len)) < 0) { |
| 2099 |
perror("accept"); |
| 2100 |
return TRUE; |
| 2101 |
} |
| 2102 |
|
| 2103 |
#ifdef G_THREADS_ENABLED |
| 2104 |
# if !GLIB_CHECK_VERSION(2, 32, 0) |
| 2105 |
g_thread_create(gntp_recv_proc, (gpointer)(intptr_t) sock, FALSE, NULL); |
| 2106 |
# else |
| 2107 |
GThread *tid = g_thread_try_new("accept", gntp_recv_proc, (gpointer)(intptr_t) sock, NULL); |
| 2108 |
g_thread_unref(tid); // detach immediately |
| 2109 |
# endif |
| 2110 |
#else |
| 2111 |
gntp_recv_proc((gpointer) sock); |
| 2112 |
#endif |
| 2113 |
return TRUE; |
| 2114 |
} |
| 2115 |
|
| 2116 |
typedef struct { |
| 2117 |
unsigned char ver; |
| 2118 |
unsigned char type; |
| 2119 |
unsigned short app_name_length; |
| 2120 |
unsigned char nall; |
| 2121 |
unsigned char ndef; |
| 2122 |
} GROWL_REGIST_PACKET; |
| 2123 |
|
| 2124 |
typedef struct { |
| 2125 |
unsigned char ver; |
| 2126 |
unsigned char type; |
| 2127 |
unsigned short flags; |
| 2128 |
unsigned short notification_length; |
| 2129 |
unsigned short title_length; |
| 2130 |
unsigned short description_length; |
| 2131 |
unsigned short app_name_length; |
| 2132 |
} GROWL_NOTIFY_PACKET; |
| 2133 |
|
| 2134 |
static gboolean |
| 2135 |
udp_recv_proc(GIOChannel* const source, GIOCondition GOL_UNUSED_ARG(condition), gpointer GOL_UNUSED_ARG(user_data)) { |
| 2136 |
int is_local_app = FALSE; |
| 2137 |
int fd = g_io_channel_unix_get_fd(source); |
| 2138 |
char buf[BUFSIZ] = {0}; |
| 2139 |
struct sockaddr_in client; |
| 2140 |
socklen_t client_len = sizeof(client); |
| 2141 |
memset(&client, 0, sizeof(client)); |
| 2142 |
|
| 2143 |
if (!getsockname(fd, (struct sockaddr *) &client, &client_len)) { |
| 2144 |
const char* addr = inet_ntoa(((struct sockaddr_in *)(void*)&client)->sin_addr); |
| 2145 |
if (addr && !strcmp(addr, "127.0.0.1")) { |
| 2146 |
is_local_app = TRUE; |
| 2147 |
} |
| 2148 |
} |
| 2149 |
const ssize_t len = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL); |
| 2150 |
if (len > 0) { |
| 2151 |
if (buf[0] == 1) { |
| 2152 |
if (buf[1] == 0 || buf[1] == 2 || buf[1] == 4) { |
| 2153 |
//GROWL_REGIST_PACKET* packet = (GROWL_REGIST_PACKET*) &buf[0]; |
| 2154 |
} else |
| 2155 |
if (buf[1] == 1 || buf[1] == 3 || buf[1] == 5) { |
| 2156 |
GROWL_NOTIFY_PACKET* packet = (GROWL_NOTIFY_PACKET*) &buf[0]; |
| 2157 |
#define HASH_DIGEST_CHECK(_hash_algorithm, _password, _data, _datalen) \ |
| 2158 |
{ \ |
| 2159 |
unsigned char digest[GOL_PP_CAT(_hash_algorithm, _DIGEST_LENGTH)] = {0}; \ |
| 2160 |
const size_t datalen = _datalen - sizeof(digest);\ |
| 2161 |
GOL_PP_CAT(_hash_algorithm, _CTX) ctx;\ |
| 2162 |
GOL_PP_CAT(_hash_algorithm, _Init)(&ctx);\ |
| 2163 |
GOL_PP_CAT(_hash_algorithm, _Update)(&ctx, _data, datalen);\ |
| 2164 |
GOL_PP_CAT(_hash_algorithm, _Update)(&ctx, _password, strlen(_password));\ |
| 2165 |
GOL_PP_CAT(_hash_algorithm, _Final)(digest, &ctx);\ |
| 2166 |
if (memcmp(digest, _data + datalen, sizeof(digest))) {\ |
| 2167 |
return TRUE;\ |
| 2168 |
}\ |
| 2169 |
} |
| 2170 |
if (packet->type == 1) { |
| 2171 |
HASH_DIGEST_CHECK(MD5, password, buf, len); |
| 2172 |
} |
| 2173 |
else if (packet->type == 3) { |
| 2174 |
HASH_DIGEST_CHECK(SHA256, password, buf, len); |
| 2175 |
} else { |
| 2176 |
if (is_local_app && require_password_for_local_apps) goto leave; |
| 2177 |
if (!is_local_app && require_password_for_lan_apps) goto leave; |
| 2178 |
} |
| 2179 |
#undef HASH_DIGEST_CHECK |
| 2180 |
NOTIFICATION_INFO* const ni = g_new0(NOTIFICATION_INFO, 1); |
| 2181 |
ni->title = g_strndup( |
| 2182 |
&buf[sizeof(GROWL_NOTIFY_PACKET) |
| 2183 |
+ ntohs(packet->notification_length)], |
| 2184 |
ntohs(packet->title_length)); |
| 2185 |
ni->text = g_strndup( |
| 2186 |
&buf[sizeof(GROWL_NOTIFY_PACKET) |
| 2187 |
+ ntohs(packet->notification_length) |
| 2188 |
+ ntohs(packet->title_length)], |
| 2189 |
ntohs(packet->description_length)); |
| 2190 |
ni->local = TRUE; |
| 2191 |
ni->timeout = get_config_value("default_timeout", 5000)/10; |
| 2192 |
g_idle_add((GSourceFunc) current_display->show, ni); // call once |
| 2193 |
} |
| 2194 |
} |
| 2195 |
} |
| 2196 |
leave: |
| 2197 |
|
| 2198 |
return TRUE; |
| 2199 |
} |
| 2200 |
|
| 2201 |
static GIOChannel* |
| 2202 |
create_udp_server() { |
| 2203 |
int fd; |
| 2204 |
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 2205 |
perror("socket"); |
| 2206 |
return NULL; |
| 2207 |
} |
| 2208 |
|
| 2209 |
const struct sockaddr_in server_addr = { |
| 2210 |
.sin_family = AF_INET, |
| 2211 |
.sin_addr.s_addr = htonl(INADDR_ANY), |
| 2212 |
.sin_port = htons(9887), |
| 2213 |
}; |
| 2214 |
|
| 2215 |
if (bind(fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { |
| 2216 |
perror("bind"); |
| 2217 |
return NULL; |
| 2218 |
} |
| 2219 |
|
| 2220 |
fd_set fdset; |
| 2221 |
FD_ZERO(&fdset); |
| 2222 |
FD_SET(fd, &fdset); |
| 2223 |
GIOChannel* const channel = g_io_channel_unix_new(fd); |
| 2224 |
g_io_add_watch(channel, G_IO_IN | G_IO_ERR, udp_recv_proc, NULL); |
| 2225 |
g_io_channel_unref(channel); |
| 2226 |
|
| 2227 |
return channel; |
| 2228 |
} |
| 2229 |
|
| 2230 |
static GIOChannel* |
| 2231 |
create_gntp_server() { |
| 2232 |
int fd; |
| 2233 |
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
| 2234 |
perror("socket"); |
| 2235 |
return NULL; |
| 2236 |
} |
| 2237 |
|
| 2238 |
const sockopt_t sockopt = 1; |
| 2239 |
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 2240 |
&sockopt, sizeof(sockopt)) == -1) { |
| 2241 |
perror("setsockopt"); |
| 2242 |
return NULL; |
| 2243 |
} |
| 2244 |
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 2245 |
&sockopt, sizeof(sockopt)) == -1) { |
| 2246 |
perror("setsockopt"); |
| 2247 |
return NULL; |
| 2248 |
} |
| 2249 |
|
| 2250 |
const struct sockaddr_in server_addr = { |
| 2251 |
.sin_family = AF_INET, |
| 2252 |
.sin_addr.s_addr = htonl(INADDR_ANY), |
| 2253 |
.sin_port = htons(23053), |
| 2254 |
}; |
| 2255 |
|
| 2256 |
if (bind(fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { |
| 2257 |
perror("bind"); |
| 2258 |
return NULL; |
| 2259 |
} |
| 2260 |
|
| 2261 |
if (listen(fd, SOMAXCONN) < 0) { |
| 2262 |
perror("listen"); |
| 2263 |
closesocket(fd); |
| 2264 |
return NULL; |
| 2265 |
} |
| 2266 |
|
| 2267 |
fd_set fdset; |
| 2268 |
FD_ZERO(&fdset); |
| 2269 |
FD_SET(fd, &fdset); |
| 2270 |
GIOChannel* const channel = g_io_channel_unix_new(fd); |
| 2271 |
g_io_add_watch(channel, G_IO_IN | G_IO_ERR, gntp_accepted, NULL); |
| 2272 |
g_io_channel_unref(channel); |
| 2273 |
|
| 2274 |
return channel; |
| 2275 |
} |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
static void |
| 2280 |
destroy_gntp_server(GIOChannel* const channel) { |
| 2281 |
if (channel) { |
| 2282 |
closesocket(g_io_channel_unix_get_fd(channel)); |
| 2283 |
g_io_channel_unref(channel); |
| 2284 |
} |
| 2285 |
} |
| 2286 |
|
| 2287 |
static void |
| 2288 |
destroy_udp_server(GIOChannel* const channel) { |
| 2289 |
if (channel) { |
| 2290 |
closesocket(g_io_channel_unix_get_fd(channel)); |
| 2291 |
g_io_channel_unref(channel); |
| 2292 |
} |
| 2293 |
} |
| 2294 |
|
| 2295 |
static void |
| 2296 |
usage(void) { |
| 2297 |
fprintf(stderr,"Usage: gol [option]\n"); |
| 2298 |
fprintf(stderr," -h : show this help\n"); |
| 2299 |
exit(1); |
| 2300 |
} |
| 2301 |
|
| 2302 |
int |
| 2303 |
main(int argc, char* argv[]) { |
| 2304 |
int ch; |
| 2305 |
|
| 2306 |
gchar* program = g_find_program_in_path(argv[0]); |
| 2307 |
exepath = g_path_get_dirname(program); |
| 2308 |
g_free(program); |
| 2309 |
|
| 2310 |
while ((ch = getopt(argc, argv, "h")) != -1) { |
| 2311 |
switch (ch){ |
| 2312 |
case 'h': |
| 2313 |
usage(); |
| 2314 |
break; |
| 2315 |
default: |
| 2316 |
usage(); |
| 2317 |
} |
| 2318 |
} |
| 2319 |
argc -= optind; |
| 2320 |
argv += optind; |
| 2321 |
|
| 2322 |
#ifdef _WIN32 |
| 2323 |
WSADATA wsaData; |
| 2324 |
WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 2325 |
#endif |
| 2326 |
GIOChannel* gntp_io = NULL; |
| 2327 |
GIOChannel* udp_io = NULL; |
| 2328 |
|
| 2329 |
#ifdef G_THREADS_ENABLED |
| 2330 |
#if !GLIB_CHECK_VERSION(2,23,2) |
| 2331 |
g_thread_init(NULL); |
| 2332 |
#endif |
| 2333 |
#endif |
| 2334 |
|
| 2335 |
g_set_application_name("growl-for-linux"); |
| 2336 |
|
| 2337 |
gtk_init(&argc, &argv); |
| 2338 |
|
| 2339 |
#ifdef _WIN32 |
| 2340 |
SetConsoleCtrlHandler(ctrl_handler, TRUE); |
| 2341 |
#else |
| 2342 |
signal(SIGTERM, signal_handler); |
| 2343 |
signal(SIGINT, signal_handler); |
| 2344 |
#endif |
| 2345 |
|
| 2346 |
if (!load_config()) goto leave; |
| 2347 |
if ((gntp_io = create_gntp_server()) == NULL) goto leave; |
| 2348 |
if ((udp_io = create_udp_server()) == NULL) goto leave; |
| 2349 |
if (!load_display_plugins()) goto leave; |
| 2350 |
if (!load_subscribe_plugins()) goto leave; |
| 2351 |
create_menu(); |
| 2352 |
|
| 2353 |
gtk_main(); |
| 2354 |
|
| 2355 |
leave: |
| 2356 |
destroy_menu(); |
| 2357 |
unload_subscribe_plugins(); |
| 2358 |
unload_display_plugins(); |
| 2359 |
destroy_gntp_server(gntp_io); |
| 2360 |
destroy_udp_server(udp_io); |
| 2361 |
unload_config(); |
| 2362 |
g_free(exepath); |
| 2363 |
|
| 2364 |
#ifdef _WIN32 |
| 2365 |
WSACleanup(); |
| 2366 |
#endif |
| 2367 |
return 0; |
| 2368 |
} |
| 2369 |
|
| 2370 |
// vim:set et sw=2 ts=2 ai: |
| 2371 |
|