privacy_dialog.c 16.5 KB
Newer Older
Stephan Arts's avatar
Stephan Arts committed
1
/*
Stephan Arts's avatar
Stephan Arts committed
2
 *  Copyright (c) Stephan Arts 2006-2012 <stephan@xfce.org>
Stephan Arts's avatar
Stephan Arts committed
3
 *
Stephan Arts's avatar
Stephan Arts committed
4 5 6 7 8
 *  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
 *  of the License, or (at your option) any later version.
 * 
Stephan Arts's avatar
Stephan Arts committed
9 10 11
 *  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
Stephan Arts's avatar
Stephan Arts committed
12 13
 *  GNU General Public License for more details.
 * 
Stephan Arts's avatar
Stephan Arts committed
14 15
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Stephan Arts's avatar
Stephan Arts committed
16 17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
Stephan Arts's avatar
Stephan Arts committed
18 19 20
 */

#include <config.h>
Igor's avatar
Igor committed
21
#include <libxfce4ui/libxfce4ui.h>
Stephan Arts's avatar
Stephan Arts committed
22 23 24 25 26 27
#include <libxfce4util/libxfce4util.h>

#include "settings.h"
#include "privacy_dialog.h"

static void
28
rstto_privacy_dialog_init (RsttoPrivacyDialog *);
Stephan Arts's avatar
Stephan Arts committed
29
static void
30
rstto_privacy_dialog_class_init (GObjectClass *);
Stephan Arts's avatar
Stephan Arts committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static void
rstto_recent_chooser_init (GtkRecentChooserIface *iface);

static void
rstto_privacy_dialog_dispose (GObject *object);

static void
rstto_privacy_dialog_set_property    (GObject      *object,
                                      guint         property_id,
                                      const GValue *value,
                                      GParamSpec   *pspec);
static void
rstto_privacy_dialog_get_property    (GObject    *object,
                                      guint       property_id,
                                      GValue     *value,
                                      GParamSpec *pspec);
47 48 49 50 51 52 53
static void
rstto_recent_chooser_add_filter (
        GtkRecentChooser  *chooser,
        GtkRecentFilter   *filter);
static GList *
rstto_recent_chooser_get_items (
        GtkRecentChooser  *chooser);
Stephan Arts's avatar
Stephan Arts committed
54 55 56

static void
cb_rstto_privacy_dialog_combobox_timeframe_changed (GtkComboBox *, gpointer user_data);
57 58 59 60
gboolean
cb_rstto_recent_filter_filter_timeframe(
        const GtkRecentFilterInfo *filter_info,
        gpointer user_data);
Stephan Arts's avatar
Stephan Arts committed
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


static GtkWidgetClass *parent_class = NULL;

enum
{
    PROP_0,
    PROP_FILTER,
    PROP_LIMIT,
    PROP_LOCAL_ONLY,
    PROP_SELECT_MULTIPLE,
    PROP_RECENT_MANAGER,
    PROP_SORT_TYPE,
    PROP_SHOW_TIPS,
    PROP_SHOW_ICONS,
    PROP_SHOW_NOT_FOUND,
    PROP_SHOW_PRIVATE,
};

struct _RsttoPrivacyDialogPriv
{
    RsttoSettings *settings;

    GtkWidget *cleanup_frame;
    GtkWidget *cleanup_vbox;
    GtkWidget *cleanup_timeframe_combo;
87 88 89 90 91 92

    GtkRecentManager *recent_manager;
    GSList           *filters;
    GtkRecentFilter  *timeframe_filter;
    time_t            time_now;
    time_t            time_offset;
Stephan Arts's avatar
Stephan Arts committed
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
};

GType
rstto_privacy_dialog_get_type (void)
{
    static GType rstto_privacy_dialog_type = 0;

    if (!rstto_privacy_dialog_type)
    {
        static const GTypeInfo rstto_privacy_dialog_info = 
        {
            sizeof (RsttoPrivacyDialogClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) rstto_privacy_dialog_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,
            sizeof (RsttoPrivacyDialog),
            0,
            (GInstanceInitFunc) rstto_privacy_dialog_init,
            NULL
        };

        static const GInterfaceInfo recent_chooser_info =
        {
            (GInterfaceInitFunc) rstto_recent_chooser_init,
            NULL,
            NULL
        };

Igor's avatar
Igor committed
123
        rstto_privacy_dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "RsttoPrivacyDialog", &rstto_privacy_dialog_info, 0);
124

Stephan Arts's avatar
Stephan Arts committed
125 126 127 128 129 130
        g_type_add_interface_static (rstto_privacy_dialog_type, GTK_TYPE_RECENT_CHOOSER, &recent_chooser_info);
    }
    return rstto_privacy_dialog_type;
}

static void
131
rstto_privacy_dialog_init (RsttoPrivacyDialog *dialog)
Stephan Arts's avatar
Stephan Arts committed
132 133 134
{
    GtkWidget *display_main_hbox;
    GtkWidget *display_main_lbl;
Igor's avatar
Igor committed
135
    GtkWidget *button;
Stephan Arts's avatar
Stephan Arts committed
136 137 138 139

    dialog->priv = g_new0 (RsttoPrivacyDialogPriv, 1);

    dialog->priv->settings = rstto_settings_new ();
Igor's avatar
Igor committed
140
    dialog->priv->time_now = time (0);
141 142 143 144 145 146 147 148 149
    dialog->priv->timeframe_filter = gtk_recent_filter_new ();

    /* Add recent-filter function to filter in access-time */    
    gtk_recent_filter_add_custom (
            dialog->priv->timeframe_filter,
            GTK_RECENT_FILTER_URI,
            (GtkRecentFilterFunc)cb_rstto_recent_filter_filter_timeframe,
            dialog,
            NULL);
Stephan Arts's avatar
Stephan Arts committed
150

Igor's avatar
Igor committed
151 152 153 154 155 156 157 158 159 160 161 162
    display_main_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
    display_main_lbl = gtk_label_new (_("Time range to clear:"));

    dialog->priv->cleanup_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
    dialog->priv->cleanup_frame = xfce_gtk_frame_box_new_with_content (_("Cleanup"), dialog->priv->cleanup_vbox);
    gtk_widget_set_margin_start (dialog->priv->cleanup_frame, 6);
    dialog->priv->cleanup_timeframe_combo = gtk_combo_box_text_new ();
    gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (dialog->priv->cleanup_timeframe_combo), 0, _("Last Hour"));
    gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (dialog->priv->cleanup_timeframe_combo), 1, _("Last Two Hours"));
    gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (dialog->priv->cleanup_timeframe_combo), 2, _("Last Four Hours"));
    gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (dialog->priv->cleanup_timeframe_combo), 3, _("Today"));
    gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (dialog->priv->cleanup_timeframe_combo), 4, _("Everything"));
Stephan Arts's avatar
Stephan Arts committed
163
    g_signal_connect (G_OBJECT (dialog->priv->cleanup_timeframe_combo), 
Igor's avatar
Igor committed
164 165
                      "changed", G_CALLBACK (cb_rstto_privacy_dialog_combobox_timeframe_changed), dialog);
    gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->priv->cleanup_timeframe_combo), 0);
Stephan Arts's avatar
Stephan Arts committed
166 167 168 169 170 171 172 173

    gtk_box_pack_start (GTK_BOX (dialog->priv->cleanup_vbox), 
                        display_main_hbox, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (display_main_hbox), 
                        display_main_lbl, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (display_main_hbox), 
                        dialog->priv->cleanup_timeframe_combo, FALSE, FALSE, 0);

174
    gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
Stephan Arts's avatar
Stephan Arts committed
175 176 177 178 179
                       dialog->priv->cleanup_frame);

    gtk_widget_show_all (dialog->priv->cleanup_frame);

    /* Window should not be resizable */
Igor's avatar
Igor committed
180 181 182 183 184 185 186 187
    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);

    button = xfce_gtk_button_new_mixed ("gtk-cancel", _("_Cancel"));
    gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_CANCEL);
    gtk_widget_show (button);
    button = xfce_gtk_button_new_mixed ("gtk-apply", _("_Apply"));
    gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
    gtk_widget_show (button);
Stephan Arts's avatar
Stephan Arts committed
188 189 190
}

static void
191
rstto_privacy_dialog_class_init (GObjectClass *object_class)
Stephan Arts's avatar
Stephan Arts committed
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
{
    GParamSpec *pspec;

    parent_class = g_type_class_peek_parent (RSTTO_PRIVACY_DIALOG_CLASS (object_class));

    object_class->dispose = rstto_privacy_dialog_dispose;

    object_class->set_property = rstto_privacy_dialog_set_property;
    object_class->get_property = rstto_privacy_dialog_get_property;

    pspec = g_param_spec_object ("filter",
                                 "",
                                 "",
                                 GTK_TYPE_RECENT_FILTER,
                                 G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_FILTER,
                                     pspec);
    pspec = g_param_spec_object ("recent-manager",
                                 "",
                                 "",
                                 GTK_TYPE_RECENT_MANAGER,
                                 G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_RECENT_MANAGER,
                                     pspec);
    pspec = g_param_spec_int    ("limit",
                                 "",
                                 "",
                                 -1,
                                 100,
                                 -1,
                                 G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_LIMIT,
                                     pspec);
    pspec = g_param_spec_boolean ("local-only",
                                  "",
                                  "",
                                  FALSE,
                                  G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_LOCAL_ONLY,
                                     pspec);

    pspec = g_param_spec_boolean ("select-multiple",
                                  "",
                                  "",
                                  FALSE,
                                  G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_SELECT_MULTIPLE,
                                     pspec);
    pspec = g_param_spec_boolean ("show-tips",
                                  "",
                                  "",
                                  FALSE,
                                  G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_SHOW_TIPS,
                                     pspec);
    pspec = g_param_spec_boolean ("show-icons",
                                  "",
                                  "",
                                  FALSE,
                                  G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_SHOW_ICONS,
                                     pspec);
    pspec = g_param_spec_boolean ("show-not-found",
                                  "",
                                  "",
                                  FALSE,
                                  G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_SHOW_NOT_FOUND,
                                     pspec);
    pspec = g_param_spec_boolean ("show-private",
                                  "",
                                  "",
                                  FALSE,
                                  G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_SHOW_PRIVATE,
                                     pspec);
    pspec = g_param_spec_enum   ("sort-type",
                                 "",
                                 "",
                                 GTK_TYPE_RECENT_SORT_TYPE,
                                 GTK_RECENT_SORT_NONE,
                                 G_PARAM_READWRITE);
    g_object_class_install_property (object_class,
                                     PROP_SORT_TYPE,
                                     pspec);
}

static void
rstto_recent_chooser_init (GtkRecentChooserIface *iface)
{
291 292
    iface->add_filter = rstto_recent_chooser_add_filter;
    iface->get_items  = rstto_recent_chooser_get_items;
Stephan Arts's avatar
Stephan Arts committed
293 294 295 296 297
}

static void
rstto_privacy_dialog_dispose (GObject *object)
{
Igor's avatar
Igor committed
298
    RsttoPrivacyDialog *dialog = RSTTO_PRIVACY_DIALOG (object);
299
    if (dialog->priv)
Stephan Arts's avatar
Stephan Arts committed
300
    {
301 302 303 304 305 306
        if (dialog->priv->settings)
        {
            g_object_unref (dialog->priv->settings);
            dialog->priv->settings = NULL;
        }

307 308 309 310 311 312
        if (dialog->priv->filters)
        {
            g_slist_free (dialog->priv->filters);
            dialog->priv->filters = NULL;
        }

313 314
        g_free (dialog->priv);
        dialog->priv = NULL;
Stephan Arts's avatar
Stephan Arts committed
315
    }
316

Stephan Arts's avatar
Stephan Arts committed
317 318 319 320 321 322 323 324 325 326
    G_OBJECT_CLASS(parent_class)->dispose(object);
}


static void
rstto_privacy_dialog_set_property    (GObject      *object,
                                      guint         property_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
327 328 329 330 331 332 333 334 335
    RsttoPrivacyDialog *dialog = RSTTO_PRIVACY_DIALOG (object);

    switch (property_id)
    {
        case PROP_RECENT_MANAGER:
            dialog->priv->recent_manager =  g_value_get_object (value);
            break;
    }

Stephan Arts's avatar
Stephan Arts committed
336 337 338 339 340 341 342 343 344
}

static void
rstto_privacy_dialog_get_property    (GObject    *object,
                                      guint       property_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
}
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

/******************************************/
/* GtkRecentChooserIface static functions */
/*                                        */
/******************************************/

static void
rstto_recent_chooser_add_filter (
        GtkRecentChooser  *chooser,
        GtkRecentFilter   *filter)
{
    RsttoPrivacyDialog *dialog = RSTTO_PRIVACY_DIALOG (chooser);

    /* Add the filter to the list of available filters */
    dialog->priv->filters = 
            g_slist_append (dialog->priv->filters, filter);
}

static GList *
rstto_recent_chooser_get_items (
        GtkRecentChooser  *chooser)
{
    RsttoPrivacyDialog *dialog = RSTTO_PRIVACY_DIALOG (chooser);
    GList *all_items = gtk_recent_manager_get_items (dialog->priv->recent_manager);
    GList *all_items_iter = all_items;
    GList *items = g_list_copy (all_items);
    GSList *filters = dialog->priv->filters;
    GtkRecentInfo *info;
    GtkRecentFilterInfo filter_info;
    gsize n_applications;

Igor's avatar
Igor committed
376
    g_list_foreach (items, (GFunc) gtk_recent_info_ref, NULL);
377 378 379 380 381 382 383

    while (NULL != all_items_iter)
    {
        info = all_items_iter->data;

        filter_info.contains = GTK_RECENT_FILTER_URI | GTK_RECENT_FILTER_APPLICATION;
        filter_info.uri = gtk_recent_info_get_uri (info);
Igor's avatar
Igor committed
384 385 386
        filter_info.applications = (const gchar **) gtk_recent_info_get_applications (info, &n_applications);

        if (!gtk_recent_filter_filter (dialog->priv->timeframe_filter, &filter_info))
387 388
        {
            items = g_list_remove (items, info);
Igor's avatar
Igor committed
389
            gtk_recent_info_unref (info);
390 391 392 393 394 395 396
        }
        else
        {
            filters = dialog->priv->filters;

            while (NULL != filters)
            {
Igor's avatar
Igor committed
397
                if (!gtk_recent_filter_filter (filters->data, &filter_info))
398 399
                {
                    items = g_list_remove (items, info);
Igor's avatar
Igor committed
400
                    gtk_recent_info_unref (info);
401 402
                    break;
                }
Igor's avatar
Igor committed
403

404 405 406 407
                filters = g_slist_next (filters);
            }
        }

Igor's avatar
Igor committed
408
        g_strfreev ((gchar **) filter_info.applications);
409 410 411
        all_items_iter = g_list_next (all_items_iter);
    }

Igor's avatar
Igor committed
412
    g_list_free_full (all_items, (GDestroyNotify) gtk_recent_info_unref);
413 414 415 416 417 418 419

    return items;
}




Stephan Arts's avatar
Stephan Arts committed
420 421 422 423 424 425 426
/***************/
/*  CALLBACKS  */
/***************/
static void
cb_rstto_privacy_dialog_combobox_timeframe_changed (GtkComboBox *combobox, gpointer user_data)
{
    RsttoPrivacyDialog *dialog = RSTTO_PRIVACY_DIALOG (user_data);
427
    struct tm *time_info;
428 429 430 431 432 433 434 435 436 437 438 439 440

    switch (gtk_combo_box_get_active (combobox))
    {
        case 0:
            dialog->priv->time_offset = 3600;
            break;
        case 1:
            dialog->priv->time_offset = 7200;
            break;
        case 2:
            dialog->priv->time_offset = 14200;
            break;
        case 3:
441 442 443 444 445 446
            /* Convert to localtime */
            time_info = localtime (&(dialog->priv->time_now));

            dialog->priv->time_offset = (time_info->tm_hour * 3600) +
                                        (time_info->tm_min * 60) + 
                                        time_info->tm_sec;
447 448 449 450 451 452 453 454 455 456 457 458 459 460
            break;
        case 4:
            dialog->priv->time_offset = dialog->priv->time_now;
            break;
    }
}

gboolean
cb_rstto_recent_filter_filter_timeframe(
        const GtkRecentFilterInfo *filter_info,
        gpointer user_data)
{
    RsttoPrivacyDialog *dialog = RSTTO_PRIVACY_DIALOG (user_data);
    GtkRecentInfo *info = gtk_recent_manager_lookup_item (dialog->priv->recent_manager, filter_info->uri, NULL);
Igor's avatar
Igor committed
461 462 463 464 465
    const time_t visited = gtk_recent_info_get_visited (info);

    gtk_recent_info_unref (info);

    return (dialog->priv->time_now - visited) < dialog->priv->time_offset;
466 467 468 469 470 471 472 473 474 475 476
}

/********************/
/* Public functions */
/********************/

GtkWidget *
rstto_privacy_dialog_new (GtkWindow *parent, GtkRecentManager *recent_manager)
{
    GtkWidget *dialog = g_object_new (RSTTO_TYPE_PRIVACY_DIALOG,
                                      "title", _("Clear private data"),
477
                                      "icon-name", "edit-clear",
478 479 480 481 482 483
                                      "recent-manager", recent_manager,
                                      NULL);

    gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);

    return dialog;
Stephan Arts's avatar
Stephan Arts committed
484
}