1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549 | /* cafe-about-me.c
* Copyright (C) 2002 Diego Gonzalez
*
* Written by: Diego Gonzalez <diego@pemas.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <unistd.h>
#include <dbus/dbus-glib-bindings.h>
#if HAVE_ACCOUNTSSERVICE
#include <act/act.h>
#endif
#define CAFE_DESKTOP_USE_UNSTABLE_API
#include <libcafe-desktop/cafe-desktop-thumbnail.h>
#include "e-image-chooser.h"
#include "cafe-about-me-password.h"
#include "cafe-about-me-fingerprint.h"
#include "marshal.h"
#include "capplet-util.h"
#define MAX_HEIGHT 100
#define MAX_WIDTH 100
typedef struct {
CtkBuilder *dialog;
CtkWidget *enable_fingerprint_button;
CtkWidget *disable_fingerprint_button;
CtkWidget *image_chooser;
GdkPixbuf *image;
#if HAVE_ACCOUNTSSERVICE
ActUser *user;
#endif
CdkScreen *screen;
CtkIconTheme *theme;
CafeDesktopThumbnailFactory *thumbs;
gboolean have_image;
gboolean image_changed;
gboolean create_self;
gchar *person;
gchar *login;
gchar *username;
guint commit_timeout_id;
} CafeAboutMe;
static CafeAboutMe *me = NULL;
static void
about_me_destroy (void)
{
if (me->dialog)
g_object_unref (me->dialog);
if (me->image)
g_object_unref (me->image);
g_free (me->person);
g_free (me->login);
g_free (me->username);
g_free (me);
me = NULL;
}
static void
about_me_load_photo (CafeAboutMe *me)
{
gchar *file = NULL;<--- Assignment 'file=NULL', assigned value is 0
GError *error = NULL;
#if HAVE_ACCOUNTSSERVICE
const gchar *act_file;<--- The scope of the variable 'act_file' can be reduced. [+]The scope of the variable 'act_file' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (act_user_is_loaded (me->user)) {
act_file = act_user_get_icon_file (me->user);
if ( act_file != NULL && strlen (act_file) > 1) {
file = g_strdup (act_file);
}
}
#endif
if (file == NULL) {<--- Condition 'file==NULL' is always true
file = g_build_filename (g_get_home_dir (), ".face", NULL);
}
me->image = gdk_pixbuf_new_from_file(file, &error);
if (me->image != NULL) {
e_image_chooser_set_from_file (E_IMAGE_CHOOSER (me->image_chooser), file);
me->have_image = TRUE;
} else {
me->have_image = FALSE;
g_warning ("Could not load %s: %s", file, error->message);
e_image_chooser_set_from_file (E_IMAGE_CHOOSER (me->image_chooser), me->person);
g_error_free (error);
}
g_free (file);
}
static void
about_me_update_photo (CafeAboutMe *me)
{
gchar *file;
GError *error = NULL;
guchar *data;
gsize length;
if (me->image_changed && me->have_image) {
GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
GdkPixbuf *pixbuf = NULL, *scaled = NULL;
int height, width;
gboolean do_scale = FALSE;
float scale = 1.0;
float scalex = 1.0, scaley = 1.0;<--- Variable 'scalex' is assigned a value that is never used.<--- Variable 'scaley' is assigned a value that is never used.
e_image_chooser_get_image_data (E_IMAGE_CHOOSER (me->image_chooser), (char **) &data, &length);
/* Before updating the image in EDS scale it to a reasonable size
so that the user doesn't get an application that does not respond
or that takes 100% CPU */
gdk_pixbuf_loader_write (loader, data, length, NULL);
gdk_pixbuf_loader_close (loader, NULL);
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (pixbuf)
g_object_ref (pixbuf);
g_object_unref (loader);
height = gdk_pixbuf_get_height (pixbuf);
width = gdk_pixbuf_get_width (pixbuf);
if (width > MAX_WIDTH) {
scalex = (float)MAX_WIDTH/width;
if (scalex < scale) {
scale = scalex;
}
do_scale = TRUE;
}
if (height > MAX_HEIGHT) {
scaley = (float)MAX_HEIGHT/height;
if (scaley < scale) {
scale = scaley;
}
do_scale = TRUE;
}
if (do_scale) {
char *scaled_data = NULL;
gsize scaled_length;
scaled = gdk_pixbuf_scale_simple (pixbuf, width*scale, height*scale, GDK_INTERP_BILINEAR);
gdk_pixbuf_save_to_buffer (scaled, &scaled_data, &scaled_length, "png", NULL,
"compression", "9", NULL);
g_free (data);
data = (guchar *) scaled_data;
length = scaled_length;
}
/* Save the image for MDM */
/* FIXME: I would have to read the default used by the mdmgreeter program */
error = NULL;
file = g_build_filename (g_get_home_dir (), ".face", NULL);
if (g_file_set_contents (file, (gchar *)data, length, &error) == TRUE) {
g_chmod (file, 0644);
#if HAVE_ACCOUNTSSERVICE
act_user_set_icon_file (me->user, file);
#endif
} else {
g_warning ("Could not create %s: %s", file, error->message);
g_error_free (error);
}
g_free (file);
g_object_unref (pixbuf);
g_free (data);
} else if (me->image_changed && !me->have_image) {
/* Update the image in the card */
file = g_build_filename (g_get_home_dir (), ".face", NULL);
g_unlink (file);
g_free (file);
#if HAVE_ACCOUNTSSERVICE
act_user_set_icon_file (me->user, "");
#endif
}
}
static void
about_me_load_info (CafeAboutMe *me)
{
set_fingerprint_label (me->enable_fingerprint_button,
me->disable_fingerprint_button);
}
static void
about_me_update_preview (CtkFileChooser *chooser,
CafeAboutMe *me)
{
gchar *uri;
uri = ctk_file_chooser_get_preview_uri (chooser);
if (uri) {
CtkWidget *image;
GdkPixbuf *pixbuf = NULL;
GFile *file;
GFileInfo *file_info;
if (!me->thumbs)
me->thumbs = cafe_desktop_thumbnail_factory_new (CAFE_DESKTOP_THUMBNAIL_SIZE_NORMAL);
file = g_file_new_for_uri (uri);
file_info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
g_object_unref (file);
if (file_info != NULL) {
const gchar *content_type;
content_type = g_file_info_get_content_type (file_info);
if (content_type) {
gchar *mime_type;
mime_type = g_content_type_get_mime_type (content_type);
pixbuf = cafe_desktop_thumbnail_factory_generate_thumbnail (me->thumbs,
uri,
mime_type);
g_free (mime_type);
}
g_object_unref (file_info);
}
image = ctk_file_chooser_get_preview_widget (chooser);
if (pixbuf != NULL) {
ctk_image_set_from_pixbuf (CTK_IMAGE (image), pixbuf);
g_object_unref (pixbuf);
} else {
ctk_image_set_from_icon_name (CTK_IMAGE (image),
"dialog-question",
CTK_ICON_SIZE_DIALOG);
}
}
ctk_file_chooser_set_preview_widget_active (chooser, TRUE);
}
static void
about_me_image_clicked_cb (CtkWidget *button, CafeAboutMe *me)
{
CtkFileChooser *chooser_dialog;
gint response;
CtkBuilder *dialog;
CtkWidget *image;
const gchar *chooser_dir = DATADIR"/pixmaps/faces";
const gchar *pics_dir;
CtkFileFilter *filter;
dialog = me->dialog;<--- Variable 'dialog' is assigned a value that is never used.
chooser_dialog = CTK_FILE_CHOOSER (
ctk_file_chooser_dialog_new (_("Select Image"), CTK_WINDOW (WID ("about-me-dialog")),
CTK_FILE_CHOOSER_ACTION_OPEN,
_("No Image"), CTK_RESPONSE_NO,
"ctk-cancel", CTK_RESPONSE_CANCEL,
"ctk-open", CTK_RESPONSE_ACCEPT,
NULL));
ctk_window_set_modal (CTK_WINDOW (chooser_dialog), TRUE);
ctk_dialog_set_default_response (CTK_DIALOG (chooser_dialog), CTK_RESPONSE_ACCEPT);
ctk_file_chooser_add_shortcut_folder (chooser_dialog, chooser_dir, NULL);
pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
if (pics_dir != NULL)
ctk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
if (!g_file_test (chooser_dir, G_FILE_TEST_IS_DIR))
chooser_dir = g_get_home_dir ();
ctk_file_chooser_set_current_folder (chooser_dialog, chooser_dir);
ctk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
image = ctk_image_new ();
ctk_file_chooser_set_preview_widget (chooser_dialog, image);
ctk_widget_set_size_request (image, 128, -1);
ctk_widget_show (image);
g_signal_connect (chooser_dialog, "update-preview",
G_CALLBACK (about_me_update_preview), me);
filter = ctk_file_filter_new ();
ctk_file_filter_set_name (filter, _("Images"));
ctk_file_filter_add_pixbuf_formats (filter);
ctk_file_chooser_add_filter (chooser_dialog, filter);
filter = ctk_file_filter_new ();
ctk_file_filter_set_name (filter, _("All Files"));
ctk_file_filter_add_pattern(filter, "*");
ctk_file_chooser_add_filter (chooser_dialog, filter);
response = ctk_dialog_run (CTK_DIALOG (chooser_dialog));
if (response == CTK_RESPONSE_ACCEPT) {
gchar* filename;
filename = ctk_file_chooser_get_filename (chooser_dialog);
me->have_image = TRUE;
me->image_changed = TRUE;
e_image_chooser_set_from_file (E_IMAGE_CHOOSER (me->image_chooser), filename);
g_free (filename);
about_me_update_photo (me);
} else if (response == CTK_RESPONSE_NO) {
me->have_image = FALSE;
me->image_changed = TRUE;
e_image_chooser_set_from_file (E_IMAGE_CHOOSER (me->image_chooser), me->person);
about_me_update_photo (me);
}
ctk_widget_destroy (CTK_WIDGET (chooser_dialog));
}
static void
about_me_image_changed_cb (CtkWidget *widget, CafeAboutMe *me)
{
me->have_image = TRUE;
me->image_changed = TRUE;
about_me_update_photo (me);
}
/* About Me Dialog Callbacks */
static void
about_me_icon_theme_changed (CtkWindow *window,
CtkIconTheme *theme)
{
CtkIconInfo *icon;
icon = ctk_icon_theme_lookup_icon (me->theme, "avatar-default", 80, 0);
if (icon != NULL) {
g_free (me->person);
me->person = g_strdup (ctk_icon_info_get_filename (icon));
g_object_unref (icon);
}
if (me->have_image) {
#if HAVE_ACCOUNTSSERVICE
act_user_set_icon_file (me->user, me->person);
#endif
e_image_chooser_set_from_file (E_IMAGE_CHOOSER (me->image_chooser), me->person);
}
}
static void
about_me_button_clicked_cb (CtkDialog *dialog, gint response_id, CafeAboutMe *me)
{
if (me->commit_timeout_id) {
g_source_remove (me->commit_timeout_id);
}
about_me_destroy ();
ctk_main_quit ();
}
static void
about_me_passwd_clicked_cb (CtkWidget *button, CafeAboutMe *me)
{
CtkBuilder *dialog;
dialog = me->dialog;<--- Variable 'dialog' is assigned a value that is never used.
cafe_about_me_password (CTK_WINDOW (WID ("about-me-dialog")));
}
static void
about_me_fingerprint_button_clicked_cb (CtkWidget *button, CafeAboutMe *me)
{
fingerprint_button_clicked (me->dialog,
me->enable_fingerprint_button,
me->disable_fingerprint_button);
}
#if HAVE_ACCOUNTSSERVICE
static void on_user_is_loaded_changed (ActUser *user, GParamSpec *pspec, CafeAboutMe* me)
{
if (act_user_is_loaded (user)) {
about_me_load_photo (me);
g_signal_handlers_disconnect_by_func (G_OBJECT (user),
G_CALLBACK (on_user_is_loaded_changed),
me);
}
}
#endif
static gint
about_me_setup_dialog (void)
{
CtkWidget *widget;
CtkWidget *main_dialog;
CtkIconInfo *icon;
CtkBuilder *dialog;
GError *error = NULL;
gchar *str;
#if HAVE_ACCOUNTSSERVICE
ActUserManager* manager;
#endif
me = g_new0 (CafeAboutMe, 1);
me->image = NULL;
dialog = ctk_builder_new ();
if (ctk_builder_add_from_resource (dialog, "/org/cafe/ccc/am/cafe-about-me-dialog.ui", &error) == 0)
{
g_warning ("Could not parse UI definition: %s", error->message);
g_error_free (error);
}
me->image_chooser = e_image_chooser_new_with_size (MAX_WIDTH, MAX_HEIGHT);
ctk_container_add (CTK_CONTAINER (WID ("button-image")), me->image_chooser);
if (dialog == NULL) {
about_me_destroy ();
return -1;
}
me->dialog = dialog;
/* Connect the close button signal */
main_dialog = WID ("about-me-dialog");
g_signal_connect (main_dialog, "response",
G_CALLBACK (about_me_button_clicked_cb), me);
ctk_window_set_resizable (CTK_WINDOW (main_dialog), FALSE);
capplet_set_icon (main_dialog, "user-info");
/* Setup theme details */
me->screen = ctk_window_get_screen (CTK_WINDOW (main_dialog));
me->theme = ctk_icon_theme_get_for_screen (me->screen);
icon = ctk_icon_theme_lookup_icon (me->theme, "avatar-default", 80, 0);
if (icon != NULL) {
me->person = g_strdup (ctk_icon_info_get_filename (icon));
g_object_unref (icon);
}
g_signal_connect_object (me->theme, "changed",
G_CALLBACK (about_me_icon_theme_changed),
main_dialog,
G_CONNECT_SWAPPED);
me->login = g_strdup (g_get_user_name ());
me->username = g_strdup (g_get_real_name ());
#if HAVE_ACCOUNTSSERVICE
manager = act_user_manager_get_default ();
me->user = act_user_manager_get_user (manager, me->login);
g_signal_connect (me->user, "notify::is-loaded", G_CALLBACK (on_user_is_loaded_changed), me);
#endif
/* Contact Tab */
about_me_load_photo (me);
widget = WID ("fullname");
str = g_strdup_printf ("<b><span size=\"xx-large\">%s</span></b>", me->username);
ctk_label_set_markup (CTK_LABEL (widget), str);
g_free (str);
widget = WID ("login");
ctk_label_set_text (CTK_LABEL (widget), me->login);
str = g_strdup_printf (_("About %s"), me->username);
ctk_window_set_title (CTK_WINDOW (main_dialog), str);
g_free (str);
widget = WID ("password");
g_signal_connect (widget, "clicked",
G_CALLBACK (about_me_passwd_clicked_cb), me);
widget = WID ("button-image");
g_signal_connect (widget, "clicked",
G_CALLBACK (about_me_image_clicked_cb), me);
me->enable_fingerprint_button = WID ("enable_fingerprint_button");
me->disable_fingerprint_button = WID ("disable_fingerprint_button");
g_signal_connect (me->enable_fingerprint_button, "clicked",
G_CALLBACK (about_me_fingerprint_button_clicked_cb), me);
g_signal_connect (me->disable_fingerprint_button, "clicked",
G_CALLBACK (about_me_fingerprint_button_clicked_cb), me);
g_signal_connect (me->image_chooser, "changed",
G_CALLBACK (about_me_image_changed_cb), me);
about_me_load_info (me);
ctk_widget_show_all (main_dialog);
return 0;
}
int
main (int argc, char **argv)
{
int rc = 0;
capplet_init (NULL, &argc, &argv);
dbus_g_object_register_marshaller (fprintd_marshal_VOID__STRING_BOOLEAN,
G_TYPE_NONE, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INVALID);
rc = about_me_setup_dialog ();
if (rc != -1) {
g_object_set (ctk_settings_get_default (), "ctk-button-images", TRUE, NULL);
ctk_main ();
}
return rc;
}
|