gtksidebarrow.c 19.9 KB
Newer Older
1 2 3 4 5 6
/* gtksidebarrow.c
 *
 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation; either version 2.1 of the
8 9 10 11 12 13 14
 * License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16 17 18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19 20
#include "config.h"

21 22 23 24 25 26 27 28 29 30
#include "gtksidebarrowprivate.h"
/* For section and place type enums */
#include "gtkplacessidebarprivate.h"
#include "gtkplacessidebar.h"
#include "gtkwidget.h"
#include "gtkimage.h"
#include "gtklabel.h"
#include "gtkstylecontext.h"
#include "gtkrevealer.h"
#include "gtkselection.h"
31

32
#ifdef HAVE_CLOUDPROVIDERS
33
#include <cloudproviders/cloudprovidersaccount.h>
34
#endif
35 36 37 38

struct _GtkSidebarRow
{
  GtkListBoxRow parent_instance;
39 40 41 42
  GIcon *start_icon;
  GIcon *end_icon;
  GtkWidget *start_icon_widget;
  GtkWidget *end_icon_widget;
43 44 45 46 47 48 49 50 51 52 53 54
  gchar *label;
  gchar *tooltip;
  GtkWidget *label_widget;
  gboolean ejectable;
  GtkWidget *eject_button;
  gint order_index;
  GtkPlacesSidebarSectionType section_type;
  GtkPlacesSidebarPlaceType place_type;
  gchar *uri;
  GDrive *drive;
  GVolume *volume;
  GMount *mount;
55
  GObject *cloud_provider_account;
56 57 58 59 60 61 62 63 64 65 66
  gboolean placeholder;
  GtkPlacesSidebar *sidebar;
  GtkWidget *event_box;
  GtkWidget *revealer;
};

G_DEFINE_TYPE (GtkSidebarRow, gtk_sidebar_row, GTK_TYPE_LIST_BOX_ROW)

enum
{
  PROP_0,
67 68
  PROP_START_ICON,
  PROP_END_ICON,
69 70 71 72 73 74 75 76 77 78 79
  PROP_LABEL,
  PROP_TOOLTIP,
  PROP_EJECTABLE,
  PROP_SIDEBAR,
  PROP_ORDER_INDEX,
  PROP_SECTION_TYPE,
  PROP_PLACE_TYPE,
  PROP_URI,
  PROP_DRIVE,
  PROP_VOLUME,
  PROP_MOUNT,
80
  PROP_CLOUD_PROVIDER_ACCOUNT,
81 82 83 84
  PROP_PLACEHOLDER,
  LAST_PROP
};

85
static GParamSpec *properties [LAST_PROP];
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
#ifdef HAVE_CLOUDPROVIDERS

static void
cloud_row_update (GtkSidebarRow *self)
{
  CloudProvidersAccount *account;
  GIcon *end_icon;
  gint provider_status;

  account = CLOUD_PROVIDERS_ACCOUNT (self->cloud_provider_account);
  provider_status = cloud_providers_account_get_status (account);
  switch (provider_status)
    {
      case CLOUD_PROVIDERS_ACCOUNT_STATUS_IDLE:
        end_icon = NULL;
        break;

      case CLOUD_PROVIDERS_ACCOUNT_STATUS_SYNCING:
        end_icon = g_themed_icon_new ("emblem-synchronizing-symbolic");
        break;

      case CLOUD_PROVIDERS_ACCOUNT_STATUS_ERROR:
        end_icon = g_themed_icon_new ("dialog-warning-symbolic");
        break;

      default:
        return;
    }

  g_object_set (self,
                "label", cloud_providers_account_get_name (account),
                NULL);
  g_object_set (self,
                "tooltip", cloud_providers_account_get_status_details (account),
                NULL);
  g_object_set (self,
                "end-icon", end_icon,
                NULL);

  if (end_icon != NULL)
    g_object_unref (end_icon);
}

#endif

132 133 134 135 136 137 138 139 140 141 142
static void
gtk_sidebar_row_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkSidebarRow *self = GTK_SIDEBAR_ROW (object);

  switch (prop_id)
    {
    case PROP_SIDEBAR:
143 144
      g_value_set_object (value, self->sidebar);
      break;
145

146 147 148 149 150 151
    case PROP_START_ICON:
      g_value_set_object (value, self->start_icon);
      break;

    case PROP_END_ICON:
      g_value_set_object (value, self->end_icon);
152
      break;
153 154

    case PROP_LABEL:
155 156
      g_value_set_string (value, self->label);
      break;
157 158

    case PROP_TOOLTIP:
159 160
      g_value_set_string (value, self->tooltip);
      break;
161 162

    case PROP_EJECTABLE:
163 164
      g_value_set_boolean (value, self->ejectable);
      break;
165 166

    case PROP_ORDER_INDEX:
167 168
      g_value_set_int (value, self->order_index);
      break;
169 170

    case PROP_SECTION_TYPE:
171 172
      g_value_set_int (value, self->section_type);
      break;
173 174

    case PROP_PLACE_TYPE:
175 176
      g_value_set_int (value, self->place_type);
      break;
177 178

    case PROP_URI:
179 180
      g_value_set_string (value, self->uri);
      break;
181 182

    case PROP_DRIVE:
183 184
      g_value_set_object (value, self->drive);
      break;
185 186

    case PROP_VOLUME:
187 188
      g_value_set_object (value, self->volume);
      break;
189 190

    case PROP_MOUNT:
191 192
      g_value_set_object (value, self->mount);
      break;
193

194 195
    case PROP_CLOUD_PROVIDER_ACCOUNT:
      g_value_set_object (value, self->cloud_provider_account);
196 197
      break;

198
    case PROP_PLACEHOLDER:
199 200
      g_value_set_boolean (value, self->placeholder);
      break;
201 202 203 204 205 206 207 208 209 210 211 212 213

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_sidebar_row_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkSidebarRow *self = GTK_SIDEBAR_ROW (object);
214
  GtkStyleContext *context;
215 216 217 218

  switch (prop_id)
    {
    case PROP_SIDEBAR:
219 220
      self->sidebar = g_value_get_object (value);
      break;
221

222 223 224 225 226 227
    case PROP_START_ICON:
      {
        g_clear_object (&self->start_icon);
        object = g_value_get_object (value);
        if (object != NULL)
          {
228
            self->start_icon = G_ICON (g_object_ref (object));
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
            gtk_image_set_from_gicon (GTK_IMAGE (self->start_icon_widget),
                                      self->start_icon,
                                      GTK_ICON_SIZE_MENU);
          }
        else
          {
            gtk_image_clear (GTK_IMAGE (self->start_icon_widget));
          }
        break;
      }

    case PROP_END_ICON:
      {
        g_clear_object (&self->end_icon);
        object = g_value_get_object (value);
        if (object != NULL)
          {
246
            self->end_icon = G_ICON (g_object_ref (object));
247 248 249 250 251 252 253 254 255 256 257 258
            gtk_image_set_from_gicon (GTK_IMAGE (self->end_icon_widget),
                                      self->end_icon,
                                      GTK_ICON_SIZE_MENU);
            gtk_widget_show (self->end_icon_widget);
          }
        else
          {
            gtk_image_clear (GTK_IMAGE (self->end_icon_widget));
            gtk_widget_hide (self->end_icon_widget);
          }
        break;
      }
259 260

    case PROP_LABEL:
261 262 263 264
      g_free (self->label);
      self->label = g_strdup (g_value_get_string (value));
      gtk_label_set_text (GTK_LABEL (self->label_widget), self->label);
      break;
265 266

    case PROP_TOOLTIP:
267 268 269 270
      g_free (self->tooltip);
      self->tooltip = g_strdup (g_value_get_string (value));
      gtk_widget_set_tooltip_text (GTK_WIDGET (self), self->tooltip);
      break;
271 272

    case PROP_EJECTABLE:
273 274 275 276 277 278
      self->ejectable = g_value_get_boolean (value);
      if (self->ejectable)
        gtk_widget_show (self->eject_button);
      else
        gtk_widget_hide (self->eject_button);
      break;
279 280

    case PROP_ORDER_INDEX:
281 282
      self->order_index = g_value_get_int (value);
      break;
283 284

    case PROP_SECTION_TYPE:
285
      self->section_type = g_value_get_int (value);
286 287
      if (self->section_type == SECTION_COMPUTER ||
          self->section_type == SECTION_OTHER_LOCATIONS)
288
        gtk_label_set_ellipsize (GTK_LABEL (self->label_widget), PANGO_ELLIPSIZE_NONE);
289 290
      else
        gtk_label_set_ellipsize (GTK_LABEL (self->label_widget), PANGO_ELLIPSIZE_END);
291
      break;
292 293

    case PROP_PLACE_TYPE:
294 295
      self->place_type = g_value_get_int (value);
      break;
296 297

    case PROP_URI:
298 299 300
      g_free (self->uri);
      self->uri = g_strdup (g_value_get_string (value));
      break;
301 302

    case PROP_DRIVE:
303 304
      g_set_object (&self->drive, g_value_get_object (value));
      break;
305 306

    case PROP_VOLUME:
307 308
      g_set_object (&self->volume, g_value_get_object (value));
      break;
309 310

    case PROP_MOUNT:
311 312
      g_set_object (&self->mount, g_value_get_object (value));
      break;
313

314
    case PROP_CLOUD_PROVIDER_ACCOUNT:
315
#ifdef HAVE_CLOUDPROVIDERS
316 317 318 319 320 321 322 323 324 325 326 327 328 329
      if (self->cloud_provider_account != NULL)
        g_signal_handlers_disconnect_by_data (self->cloud_provider_account, self);

      self->cloud_provider_account = g_value_dup_object (value);

      if (self->cloud_provider_account != NULL)
        {
          g_signal_connect_swapped (self->cloud_provider_account, "notify::name",
                                    G_CALLBACK (cloud_row_update), self);
          g_signal_connect_swapped (self->cloud_provider_account, "notify::status",
                                    G_CALLBACK (cloud_row_update), self);
          g_signal_connect_swapped (self->cloud_provider_account, "notify::status-details",
                                    G_CALLBACK (cloud_row_update), self);
        }
330
#endif
331
      break;
332

333
    case PROP_PLACEHOLDER:
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
      {
        self->placeholder = g_value_get_boolean (value);
        if (self->placeholder)
          {
            g_clear_object (&self->start_icon);
            g_clear_object (&self->end_icon);
            g_free (self->label);
            self->label = NULL;
            g_free (self->tooltip);
            self->tooltip = NULL;
            gtk_widget_set_tooltip_text (GTK_WIDGET (self), NULL);
            self->ejectable = FALSE;
            self->section_type = SECTION_BOOKMARKS;
            self->place_type = PLACES_BOOKMARK_PLACEHOLDER;
            g_free (self->uri);
            self->uri = NULL;
            g_clear_object (&self->drive);
            g_clear_object (&self->volume);
            g_clear_object (&self->mount);
353
            g_clear_object (&self->cloud_provider_account);
354 355 356 357 358 359 360 361 362 363 364

            gtk_container_foreach (GTK_CONTAINER (self),
                                   (GtkCallback) gtk_widget_destroy,
                                   NULL);

            context = gtk_widget_get_style_context (GTK_WIDGET (self));
            gtk_style_context_add_class (context, "sidebar-placeholder-row");
          }

        break;
      }
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

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
on_child_revealed (GObject    *self,
                   GParamSpec *pspec,
                   gpointer    user_data)
{
 /* We need to hide the actual widget because if not the GtkListBoxRow will
  * still allocate the paddings, even if the revealer is not revealed, and
  * therefore the row will be still somewhat visible. */
  if (!gtk_revealer_get_reveal_child (GTK_REVEALER (self)))
    gtk_widget_hide (GTK_WIDGET (GTK_SIDEBAR_ROW (user_data)));
}

void
gtk_sidebar_row_reveal (GtkSidebarRow *self)
{
  gtk_widget_show_all (GTK_WIDGET (self));
  gtk_revealer_set_reveal_child (GTK_REVEALER (self->revealer), TRUE);
}

void
gtk_sidebar_row_hide (GtkSidebarRow *self,
                      gboolean       inmediate)
{
  guint transition_duration;

  transition_duration = gtk_revealer_get_transition_duration (GTK_REVEALER (self->revealer));
  if (inmediate)
      gtk_revealer_set_transition_duration (GTK_REVEALER (self->revealer), 0);

  gtk_revealer_set_reveal_child (GTK_REVEALER (self->revealer), FALSE);

  gtk_revealer_set_transition_duration (GTK_REVEALER (self->revealer), transition_duration);
}

405
void
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
gtk_sidebar_row_set_start_icon (GtkSidebarRow *self,
                                GIcon         *icon)
{
  g_return_if_fail (GTK_IS_SIDEBAR_ROW (self));

  if (self->start_icon != icon)
    {
      g_set_object (&self->start_icon, icon);
      if (self->start_icon != NULL)
        gtk_image_set_from_gicon (GTK_IMAGE (self->start_icon_widget), self->start_icon,
                                  GTK_ICON_SIZE_MENU);
      else
        gtk_image_clear (GTK_IMAGE (self->start_icon_widget));

      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_START_ICON]);
    }
}

void
gtk_sidebar_row_set_end_icon (GtkSidebarRow *self,
                              GIcon         *icon)
427 428 429
{
  g_return_if_fail (GTK_IS_SIDEBAR_ROW (self));

430
  if (self->end_icon != icon)
431
    {
432 433 434
      g_set_object (&self->end_icon, icon);
      if (self->end_icon != NULL)
        gtk_image_set_from_gicon (GTK_IMAGE (self->end_icon_widget), self->end_icon,
435 436
                                  GTK_ICON_SIZE_MENU);
      else
437 438
        if (self->end_icon_widget != NULL)
          gtk_image_clear (GTK_IMAGE (self->end_icon_widget));
439

440
      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_END_ICON]);
441 442 443
    }
}

444 445 446 447 448
static void
gtk_sidebar_row_finalize (GObject *object)
{
  GtkSidebarRow *self = GTK_SIDEBAR_ROW (object);

449 450
  g_clear_object (&self->start_icon);
  g_clear_object (&self->end_icon);
451 452 453 454 455 456 457 458 459
  g_free (self->label);
  self->label = NULL;
  g_free (self->tooltip);
  self->tooltip = NULL;
  g_free (self->uri);
  self->uri = NULL;
  g_clear_object (&self->drive);
  g_clear_object (&self->volume);
  g_clear_object (&self->mount);
460 461 462 463 464
#ifdef HAVE_CLOUDPROVIDERS
  if (self->cloud_provider_account != NULL)
    g_signal_handlers_disconnect_by_data (self->cloud_provider_account, self);
  g_clear_object (&self->cloud_provider_account);
#endif
465 466

  G_OBJECT_CLASS (gtk_sidebar_row_parent_class)->finalize (object);
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
}

static void
gtk_sidebar_row_init (GtkSidebarRow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
}

static void
gtk_sidebar_row_class_init (GtkSidebarRowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->get_property = gtk_sidebar_row_get_property;
  object_class->set_property = gtk_sidebar_row_set_property;
  object_class->finalize = gtk_sidebar_row_finalize;

485
  properties [PROP_SIDEBAR] =
486 487 488 489 490 491 492
    g_param_spec_object ("sidebar",
                         "Sidebar",
                         "Sidebar",
                         GTK_TYPE_PLACES_SIDEBAR,
                         (G_PARAM_READWRITE |
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));
493

494 495 496 497 498 499 500 501 502 503 504 505
  properties [PROP_START_ICON] =
    g_param_spec_object ("start-icon",
                         "start-icon",
                         "The start icon.",
                         G_TYPE_ICON,
                         (G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));

  properties [PROP_END_ICON] =
    g_param_spec_object ("end-icon",
                         "end-icon",
                         "The end icon.",
506 507 508 509
                         G_TYPE_ICON,
                         (G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));

510
  properties [PROP_LABEL] =
511 512 513 514 515 516 517
    g_param_spec_string ("label",
                         "label",
                         "The label text.",
                         NULL,
                         (G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));

518
  properties [PROP_TOOLTIP] =
519 520 521 522 523 524 525
    g_param_spec_string ("tooltip",
                         "Tooltip",
                         "Tooltip",
                         NULL,
                         (G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));

526
  properties [PROP_EJECTABLE] =
527 528 529 530 531 532 533
    g_param_spec_boolean ("ejectable",
                          "Ejectable",
                          "Ejectable",
                          FALSE,
                          (G_PARAM_READWRITE |
                           G_PARAM_STATIC_STRINGS));

534
  properties [PROP_ORDER_INDEX] =
535 536 537 538 539 540 541
    g_param_spec_int ("order-index",
                      "OrderIndex",
                      "Order Index",
                      0, G_MAXINT, 0,
                      (G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS));

542
  properties [PROP_SECTION_TYPE] =
543 544 545 546 547 548 549 550
    g_param_spec_int ("section-type",
                      "section type",
                      "The section type.",
                      SECTION_INVALID, N_SECTIONS, SECTION_INVALID,
                      (G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS |
                       G_PARAM_CONSTRUCT_ONLY));

551
  properties [PROP_PLACE_TYPE] =
552 553 554 555 556 557 558 559
    g_param_spec_int ("place-type",
                      "place type",
                      "The place type.",
                      PLACES_INVALID, N_PLACES, PLACES_INVALID,
                      (G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS |
                       G_PARAM_CONSTRUCT_ONLY));

560
  properties [PROP_URI] =
561 562 563 564 565 566 567
    g_param_spec_string ("uri",
                         "Uri",
                         "Uri",
                         NULL,
                         (G_PARAM_READWRITE |
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));
568 569

  properties [PROP_DRIVE] =
570 571 572 573 574 575 576 577
    g_param_spec_object ("drive",
                         "Drive",
                         "Drive",
                         G_TYPE_DRIVE,
                         (G_PARAM_READWRITE |
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));

578
  properties [PROP_VOLUME] =
579 580 581 582 583 584 585 586
    g_param_spec_object ("volume",
                         "Volume",
                         "Volume",
                         G_TYPE_VOLUME,
                         (G_PARAM_READWRITE |
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));

587
  properties [PROP_MOUNT] =
588 589 590 591 592 593 594
    g_param_spec_object ("mount",
                         "Mount",
                         "Mount",
                         G_TYPE_MOUNT,
                         (G_PARAM_READWRITE |
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));
595

596 597 598 599
  properties [PROP_CLOUD_PROVIDER_ACCOUNT] =
    g_param_spec_object ("cloud-provider-account",
                         "CloudProvidersAccount",
                         "CloudProvidersAccount",
600 601 602 603
                         G_TYPE_OBJECT,
                         (G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));

604
  properties [PROP_PLACEHOLDER] =
605 606 607 608 609 610 611
    g_param_spec_boolean ("placeholder",
                          "Placeholder",
                          "Placeholder",
                          FALSE,
                          (G_PARAM_READWRITE |
                           G_PARAM_CONSTRUCT_ONLY |
                           G_PARAM_STATIC_STRINGS));
612 613

  g_object_class_install_properties (object_class, LAST_PROP, properties);
614 615 616 617

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/org/gtk/libgtk/ui/gtksidebarrow.ui");

618 619
  gtk_widget_class_bind_template_child (widget_class, GtkSidebarRow, start_icon_widget);
  gtk_widget_class_bind_template_child (widget_class, GtkSidebarRow, end_icon_widget);
620 621 622 623 624 625
  gtk_widget_class_bind_template_child (widget_class, GtkSidebarRow, label_widget);
  gtk_widget_class_bind_template_child (widget_class, GtkSidebarRow, eject_button);
  gtk_widget_class_bind_template_child (widget_class, GtkSidebarRow, event_box);
  gtk_widget_class_bind_template_child (widget_class, GtkSidebarRow, revealer);

  gtk_widget_class_bind_template_callback (widget_class, on_child_revealed);
626
  gtk_widget_class_set_css_name (widget_class, "row");
627 628 629 630 631
}

GtkSidebarRow*
gtk_sidebar_row_clone (GtkSidebarRow *self)
{
632 633
 return g_object_new (GTK_TYPE_SIDEBAR_ROW,
                      "sidebar", self->sidebar,
634 635
                      "start-icon", self->start_icon,
                      "end-icon", self->end_icon,
636 637 638 639 640 641 642 643 644 645
                      "label", self->label,
                      "tooltip", self->tooltip,
                      "ejectable", self->ejectable,
                      "order-index", self->order_index,
                      "section-type", self->section_type,
                      "place-type", self->place_type,
                      "uri", self->uri,
                      "drive", self->drive,
                      "volume", self->volume,
                      "mount", self->mount,
646
                      "cloud-provider-account", self->cloud_provider_account,
647 648 649
                      NULL);
}

650
GtkWidget*
651 652 653 654 655
gtk_sidebar_row_get_eject_button (GtkSidebarRow *self)
{
  return self->eject_button;
}

656
GtkWidget*
657 658 659
gtk_sidebar_row_get_event_box (GtkSidebarRow *self)
{
  return self->event_box;
660
}