generalsettingsview.cpp 18.6 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
 *
 *  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 3 of the License, 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 Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#include "generalsettingsview.h"

22
// GTK+ related
23
#include <gtk/gtk.h>
24
#include <glib/gi18n.h>
25
#include <glib.h>
26

27 28 29
// LRC
#include <api/avmodel.h>

30
// Jami Client
31 32
#include "utils/files.h"
#include "avatarmanipulation.h"
33

34 35 36 37
namespace { namespace details {
class CppImpl;
}}

38 39
enum
{
40
  PROP_MAIN_WIN_PNT = 1,
41 42
};

43 44
struct _GeneralSettingsView
{
45
    GtkScrolledWindow parent;
46 47 48 49
};

struct _GeneralSettingsViewClass
{
50
    GtkScrolledWindowClass parent_class;
51 52 53 54 55 56
};

typedef struct _GeneralSettingsViewPrivate GeneralSettingsViewPrivate;

struct _GeneralSettingsViewPrivate
{
57 58
    GSettings *settings;

59
    /* Jami settings */
60 61 62 63 64 65 66 67
    GtkWidget *at_startup_button;
    GtkWidget *systray_button;
    GtkWidget *incoming_open_button;
    GtkWidget *call_notifications_button;
    GtkWidget *request_notifications_button;
    GtkWidget *chat_notifications_button;
    GtkWidget *media_chatview_button;
    GtkWidget *chatview_pos_button;
68
    GtkWidget *button_choose_downloads_directory;
69

70 71 72
    /* history settings */
    GtkWidget *adjustment_history_duration;
    GtkWidget *button_clear_history;
73

74 75 76 77 78 79
    /* Call recording settings */
    GtkWidget *record_preview_button;
    GtkWidget *always_record_button;
    GtkWidget *adjustment_record_quality;
    GtkWidget *filechooserbutton_record_path;

80
    /* ring main window pointer */
81
    GtkWidget* main_window_pnt;
82 83

    details::CppImpl* cpp; ///< Non-UI and C++ only code
84 85
};

86
G_DEFINE_TYPE_WITH_PRIVATE(GeneralSettingsView, general_settings_view, GTK_TYPE_SCROLLED_WINDOW);
87 88 89

#define GENERAL_SETTINGS_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GENERAL_SETTINGS_VIEW_TYPE, GeneralSettingsViewPrivate))

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
namespace { namespace details {

class CppImpl
{
public:
    explicit CppImpl(GeneralSettingsView& widget, lrc::api::AVModel& avModel);

    lrc::api::AVModel* avModel_ = nullptr;
    GeneralSettingsView* self = nullptr; // The GTK widget itself
    GeneralSettingsViewPrivate* widgets = nullptr;
};

CppImpl::CppImpl(GeneralSettingsView& widget, lrc::api::AVModel& avModel)
    : self(&widget)
    , widgets(GENERAL_SETTINGS_VIEW_GET_PRIVATE(&widget))
    , avModel_(&avModel)
{
    gtk_switch_set_active(
        GTK_SWITCH(widgets->record_preview_button),
        avModel_->getRecordPreview());
    gtk_switch_set_active(
        GTK_SWITCH(widgets->always_record_button),
        avModel_->getAlwaysRecord());
    gtk_file_chooser_set_action(
        GTK_FILE_CHOOSER(widgets->filechooserbutton_record_path),
        GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
    gtk_file_chooser_set_filename(
        GTK_FILE_CHOOSER(widgets->filechooserbutton_record_path),
        avModel_->getRecordPath().c_str());
    gtk_adjustment_set_value(
        GTK_ADJUSTMENT(widgets->adjustment_record_quality),
        avModel_->getRecordQuality());
}

}} // namespace details

126 127
enum {
    CLEAR_ALL_HISTORY,
128
    UPDATE_DOWNLOAD_FOLDER,
129 130 131 132 133
    LAST_SIGNAL
};

static guint general_settings_view_signals[LAST_SIGNAL] = { 0 };

134 135 136
static void
general_settings_view_dispose(GObject *object)
{
137 138 139 140
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(object);

    g_clear_object(&priv->settings);

141 142 143 144 145 146 147 148 149 150
    G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
}

static gboolean
clear_history_dialog(GeneralSettingsView *self)
{
    gboolean response = FALSE;
    GtkWidget *dialog = gtk_message_dialog_new(NULL,
                            (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
                            GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
151
                            _("This is a destructive operation. Are you sure you want to delete all of your chat and call history?"));
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

    gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);

    /* get parent window so we can center on it */
    GtkWidget *parent = gtk_widget_get_toplevel(GTK_WIDGET(self));
    if (gtk_widget_is_toplevel(parent)) {
        gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
        gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
    }

    switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
        case GTK_RESPONSE_OK:
            response = TRUE;
            break;
        default:
            response = FALSE;
            break;
    }

    gtk_widget_destroy(dialog);

    return response;
}

static void
clear_history(G_GNUC_UNUSED GtkWidget *button, GeneralSettingsView *self)
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));

181 182
    if (clear_history_dialog(self) )
        g_signal_emit(G_OBJECT(self), general_settings_view_signals[CLEAR_ALL_HISTORY], 0);
183 184
}

185
static void
186 187 188 189 190 191 192 193 194 195
update_downloads_button_label(GeneralSettingsView *self)
{
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);

    // Get folder name
    const gchar *folder_dirname = g_path_get_basename(g_variant_get_string(g_settings_get_value(priv->settings, "download-folder"), NULL));
    gtk_button_set_label(GTK_BUTTON(priv->button_choose_downloads_directory), folder_dirname);
}

static void
196
change_prefered_directory (gchar * directory, GeneralSettingsView *self, gchar * id, void (*cb)(GeneralSettingsView*))
197 198 199 200
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);

201
    priv->settings = g_settings_new_full(get_settings_schema(), NULL, NULL);
202 203
    g_settings_set_value(priv->settings, id, g_variant_new("s", directory));
    cb(self);
204 205 206 207 208 209 210 211 212 213 214 215
}

static void
choose_downloads_directory(GeneralSettingsView *self)
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));

    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);

    gint res;
    gchar* filename = nullptr;

216
    if (!priv->main_window_pnt) {
217 218 219 220 221
        g_debug("Internal error: NULL main window pointer in GeneralSettingsView.");
        return;
    }

    GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Choose download folder"),
222
                                      GTK_WINDOW(priv->main_window_pnt),
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                                      GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                      _("_Cancel"),
                                      GTK_RESPONSE_CANCEL,
                                      _("_Save"),
                                      GTK_RESPONSE_ACCEPT,
                                      NULL);

    res = gtk_dialog_run (GTK_DIALOG(dialog));

    if (res == GTK_RESPONSE_ACCEPT) {
        auto chooser = GTK_FILE_CHOOSER(dialog);
        filename = gtk_file_chooser_get_filename(chooser);
    }
    gtk_widget_destroy (dialog);

    if (!filename) return;

    // set download folder
241
    change_prefered_directory(filename, self, "download-folder", update_downloads_button_label);
242 243

    g_signal_emit(G_OBJECT(self), general_settings_view_signals[UPDATE_DOWNLOAD_FOLDER], 0, filename);
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
static void
preview_toggled(GtkSwitch *button, GParamSpec* /*spec*/, GeneralSettingsView *self)
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
    gboolean preview = gtk_switch_get_active(button);
    if (priv && priv->cpp && priv->cpp->avModel_)
        priv->cpp->avModel_->setRecordPreview(preview);
}

static void
always_record_toggled(GtkSwitch *button, GParamSpec* /*spec*/, GeneralSettingsView *self)
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
    gboolean rec = gtk_switch_get_active(button);
    if (priv && priv->cpp && priv->cpp->avModel_)
        priv->cpp->avModel_->setAlwaysRecord(rec);
}

static void
record_quality_changed(GtkAdjustment *adjustment, GeneralSettingsView *self)
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
    gdouble qual = gtk_adjustment_get_value(adjustment);
    if (priv && priv->cpp && priv->cpp->avModel_)
        priv->cpp->avModel_->setRecordQuality(qual);
}

static void
update_record_path(GtkFileChooser *file_chooser, GeneralSettingsView* self)
{
    g_return_if_fail(IS_GENERAL_SETTINGS_VIEW(self));
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
    auto* filename = gtk_file_chooser_get_filename(file_chooser);
    if (filename != priv->cpp->avModel_->getRecordPath())
        priv->cpp->avModel_->setRecordPath(filename);
    g_free(filename);
}

287 288 289 290 291 292 293
static void
general_settings_view_init(GeneralSettingsView *self)
{
    gtk_widget_init_template(GTK_WIDGET(self));

    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);

294
    priv->settings = g_settings_new_full(get_settings_schema(), NULL, NULL);
295

296 297 298 299
    GtkStyleContext* context;
    context = gtk_widget_get_style_context(GTK_WIDGET(priv->button_clear_history));
    gtk_style_context_add_class(context, "button_red");

300
    /* bind client option to gsettings */
301
    g_settings_bind(priv->settings, "start-on-login",
302
                    priv->at_startup_button, "active",
303
                    G_SETTINGS_BIND_DEFAULT);
304
    g_settings_bind(priv->settings, "show-status-icon",
305
                    priv->systray_button, "active",
306 307
                    G_SETTINGS_BIND_DEFAULT);
    g_settings_bind(priv->settings, "bring-window-to-front",
308
                    priv->incoming_open_button, "active",
309
                    G_SETTINGS_BIND_DEFAULT);
310
    g_settings_bind(priv->settings, "enable-call-notifications",
311
                    priv->call_notifications_button, "active",
312
                    G_SETTINGS_BIND_DEFAULT);
313
    g_settings_bind(priv->settings, "enable-display-links",
314
                    priv->media_chatview_button, "active",
315
                    G_SETTINGS_BIND_DEFAULT);
316
    g_settings_bind(priv->settings, "enable-pending-notifications",
317
                    priv->request_notifications_button, "active",
318
                    G_SETTINGS_BIND_DEFAULT);
319
    g_settings_bind(priv->settings, "enable-chat-notifications",
320
                    priv->chat_notifications_button, "active",
321
                    G_SETTINGS_BIND_DEFAULT);
322
    g_settings_bind(priv->settings, "chat-pane-horizontal",
323
                    priv->chatview_pos_button, "active",
324
                    G_SETTINGS_BIND_DEFAULT);
Nicolas Jager's avatar
Nicolas Jager committed
325 326 327
    g_settings_bind(priv->settings, "history-limit",
                    priv->adjustment_history_duration, "value",
                    G_SETTINGS_BIND_DEFAULT);
328

329
    /* Set-up download directory settings, default if none specified */
330 331 332
    auto* download_directory_variant = g_settings_get_value(priv->settings, "download-folder");
    char* download_directory_value;
    g_variant_get(download_directory_variant, "&s", &download_directory_value);
333
    std::string default_download_dir = {};
334 335 336
    if (auto* directory = g_get_home_dir ())
        default_download_dir = directory;
    else if (auto* directory = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD))
337 338 339
        default_download_dir = directory;
    else
        default_download_dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
340 341 342 343
    auto current_value = std::string(download_directory_value);
    if (current_value.empty()) {
        g_settings_set_value(priv->settings, "download-folder", g_variant_new("s", default_download_dir.c_str()));
    }
344
    update_downloads_button_label(self);
345

346 347
    /* clear history */
    g_signal_connect(priv->button_clear_history, "clicked", G_CALLBACK(clear_history), self);
348 349 350 351 352 353

    /* Recording callbacks */
    g_signal_connect(priv->record_preview_button, "notify::active", G_CALLBACK(preview_toggled), self);
    g_signal_connect(priv->always_record_button, "notify::active", G_CALLBACK(always_record_toggled), self);
    g_signal_connect(priv->adjustment_record_quality, "value-changed", G_CALLBACK(record_quality_changed), self);
    g_signal_connect(priv->filechooserbutton_record_path, "file-set", G_CALLBACK(update_record_path), self);
354 355 356 357 358 359 360 361 362 363 364 365
}


static void
general_settings_view_set_property (GObject      *object,
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
    GeneralSettingsView *self = GENERAL_SETTINGS_VIEW (object);
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);

366 367
    if (property_id == PROP_MAIN_WIN_PNT) {
        GtkWidget *main_window_pnt = (GtkWidget*) g_value_get_pointer(value);
368

369
        if (!main_window_pnt) {
370 371 372 373
            g_debug("Internal error: NULL main window pointer passed to set_property");
            return;
        }

374
        priv->main_window_pnt = main_window_pnt;
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    }
    else {
        // Invalid property id passed
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
general_settings_view_get_property (GObject    *object,
                                    guint       property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
    GeneralSettingsView *self = GENERAL_SETTINGS_VIEW (object);
    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);

391 392
    if (property_id == PROP_MAIN_WIN_PNT) {
        g_value_set_pointer(value, priv->main_window_pnt);
393 394 395 396 397
    }
    else {
        // Invalid property id passed
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
398 399 400 401 402
}

static void
general_settings_view_class_init(GeneralSettingsViewClass *klass)
{
403 404
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    object_class->dispose = general_settings_view_dispose;
405 406

    gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
407
                                                "/net/jami/JamiGnome/generalsettingsview.ui");
408

409 410 411 412 413 414 415 416
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, at_startup_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, systray_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, incoming_open_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, call_notifications_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, media_chatview_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, request_notifications_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, chat_notifications_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, chatview_pos_button);
417 418
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
419
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_choose_downloads_directory);
420 421 422 423
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, record_preview_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, always_record_button);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_record_quality);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, filechooserbutton_record_path);
424 425 426 427 428 429 430 431 432 433 434

    general_settings_view_signals[CLEAR_ALL_HISTORY] = g_signal_new (
        "clear-all-history",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

435 436 437 438 439 440 441 442 443 444
    general_settings_view_signals[UPDATE_DOWNLOAD_FOLDER] = g_signal_new("update-download-folder",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__BOOLEAN,
        G_TYPE_NONE,
        1, G_TYPE_STRING);

445 446 447
    /* Define class properties: e.g. pointer to main window, etc.*/
    object_class->set_property = general_settings_view_set_property;
    object_class->get_property = general_settings_view_get_property;
448

449
    GParamFlags flags = (GParamFlags) (G_PARAM_READWRITE);
450 451 452 453
    g_object_class_install_property (object_class, PROP_MAIN_WIN_PNT,
        g_param_spec_pointer ("main_window_pnt",
                              "MainWindow pointer",
                              "Pointer to the Main Window. This property is used by modal dialogs.",
454
                              flags));
455 456 457
}

GtkWidget *
458
general_settings_view_new(GtkWidget* main_window_pointer, lrc::api::AVModel& avModel)
459
{
460 461 462 463 464
    auto self = g_object_new(GENERAL_SETTINGS_VIEW_TYPE, NULL);
    auto* priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(GENERAL_SETTINGS_VIEW (self));
    priv->cpp = new details::CppImpl(
        *(reinterpret_cast<GeneralSettingsView*>(self)), avModel
    );
465

466 467 468
    // set_up ring main window pointer (needed by modal dialogs)
    GValue val = G_VALUE_INIT;
    g_value_init (&val, G_TYPE_POINTER);
469 470
    g_value_set_pointer (&val, main_window_pointer);
    g_object_set_property (G_OBJECT (self), "main_window_pnt", &val);
471 472
    g_value_unset (&val);

473
    g_signal_connect_swapped(priv->button_choose_downloads_directory, "clicked", G_CALLBACK(choose_downloads_directory), self);
474

475 476 477 478 479 480 481 482
    // CSS styles
    auto provider = gtk_css_provider_new();
    std::string css = ".button_red { color: white; background: #dc3a37; border: 0; }";
    css += ".button_red:hover { background: #dc2719; }";
    gtk_css_provider_load_from_data(provider, css.c_str(), -1, nullptr);
    gtk_style_context_add_provider_for_screen(gdk_display_get_default_screen(gdk_display_get_default()),
                                              GTK_STYLE_PROVIDER(provider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
483
    return (GtkWidget *)self;
484
}