gtd-sidebar.c 25.9 KB
Newer Older
1 2
/* gtd-sidebar.c
 *
3
 * Copyright 2018-2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#define G_LOG_DOMAIN "GtdSidebar"

#include "gtd-debug.h"
#include "gtd-manager.h"
25
#include "gtd-max-size-layout.h"
26 27 28 29 30 31 32
#include "gtd-panel.h"
#include "gtd-provider.h"
#include "gtd-sidebar.h"
#include "gtd-sidebar-list-row.h"
#include "gtd-sidebar-panel-row.h"
#include "gtd-sidebar-provider-row.h"
#include "gtd-task-list.h"
33
#include "gtd-task-list-panel.h"
34
#include "gtd-utils.h"
35 36 37
#include "notification/gtd-notification.h"

#include <glib/gi18n.h>
38 39 40

struct _GtdSidebar
{
41
  GtdWidget           parent;
42

43 44
  GtkListBox         *archive_listbox;
  GtkListBoxRow      *archive_row;
45
  GtkListBox         *listbox;
46
  GtkStack           *stack;
47

48
  GtkStack           *panel_stack;
49
  GtdPanel           *task_list_panel;
50 51

  GSimpleActionGroup *action_group;
52 53
};

54
G_DEFINE_TYPE (GtdSidebar, gtd_sidebar, GTD_TYPE_WIDGET)
55 56 57 58 59 60


/*
 * Auxiliary methods
 */

61
static gboolean
62 63 64 65
activate_row_below (GtdSidebar        *self,
                    GtdSidebarListRow *current_row)
{
  GtkWidget *next_row;
66
  GtkWidget *parent;
67
  GtkWidget *child;
68 69
  gboolean after_deleted;

70
  parent = gtk_widget_get_parent (GTK_WIDGET (current_row));
71 72 73
  after_deleted = FALSE;
  next_row = NULL;

74 75 76
  for (child = gtk_widget_get_first_child (parent);
       child;
       child = gtk_widget_get_next_sibling (child))
77
    {
78
      if (child == (GtkWidget*) current_row)
79 80 81 82 83
        {
          after_deleted = TRUE;
          continue;
        }

84 85
      if (!gtk_widget_get_visible (child) ||
          !gtk_list_box_row_get_activatable (GTK_LIST_BOX_ROW (child)))
86 87 88 89
        {
          continue;
        }

90
      next_row = child;
91 92 93 94 95 96 97

      if (after_deleted)
        break;
    }

  if (next_row)
    g_signal_emit_by_name (next_row, "activate");
98 99

  return next_row != NULL;
100 101
}

102 103 104 105
static void
add_task_list (GtdSidebar  *self,
               GtdTaskList *list)
{
106 107 108
  if (gtd_task_list_is_inbox (list))
    return;

109 110
  g_debug ("Adding task list '%s'", gtd_task_list_get_name (list));

111
  if (!gtd_task_list_get_archived (list))
112 113 114 115
    {
      gtk_list_box_prepend (self->listbox, gtd_sidebar_list_row_new (list));
      gtk_list_box_invalidate_filter (self->listbox);
    }
116
  else
117 118 119 120
    {
      gtk_list_box_prepend (self->archive_listbox, gtd_sidebar_list_row_new (list));
      gtk_list_box_invalidate_filter (self->archive_listbox);
    }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
}

static void
add_panel (GtdSidebar *self,
           GtdPanel   *panel)
{
  GtkWidget *row;

  g_debug ("Adding panel '%s'", gtd_panel_get_panel_name (panel));

  row = gtd_sidebar_panel_row_new (panel);

  gtk_list_box_prepend (self->listbox, row);
}

static void
add_provider (GtdSidebar  *self,
              GtdProvider *provider)
{
  g_debug ("Adding provider '%s'", gtd_provider_get_name (provider));

142 143
  gtk_list_box_prepend (self->listbox, gtd_sidebar_provider_row_new (provider));
  gtk_list_box_prepend (self->archive_listbox, gtd_sidebar_provider_row_new (provider));
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
}

static gint
compare_panels (GtdSidebarPanelRow *row_a,
                GtdSidebarPanelRow *row_b)
{
  GtdPanel *panel_a;
  GtdPanel *panel_b;

  panel_a = gtd_sidebar_panel_row_get_panel (row_a);
  panel_b = gtd_sidebar_panel_row_get_panel (row_b);

  return gtd_panel_get_priority (panel_b) - gtd_panel_get_priority (panel_a);
}

static gint
compare_providers (GtdSidebarProviderRow *row_a,
                   GtdSidebarProviderRow *row_b)
{
  GtdProvider *provider_a;
  GtdProvider *provider_b;

  provider_a = gtd_sidebar_provider_row_get_provider (row_a);
  provider_b = gtd_sidebar_provider_row_get_provider (row_b);

  return gtd_provider_compare (provider_a, provider_b);
}

static gint
compare_lists (GtdSidebarListRow *row_a,
               GtdSidebarListRow *row_b)
{
  GtdTaskList *list_a;
  GtdTaskList *list_b;
  gint result;

  list_a = gtd_sidebar_list_row_get_task_list (row_a);
  list_b = gtd_sidebar_list_row_get_task_list (row_b);

  /* First, compare by their providers */
  result = gtd_provider_compare (gtd_task_list_get_provider (list_a), gtd_task_list_get_provider (list_b));

  if (result != 0)
    return result;

  return gtd_collate_compare_strings (gtd_task_list_get_name (list_a), gtd_task_list_get_name (list_b));
}

typedef gpointer (*GetDataFunc) (gpointer data);

static gpointer
get_row_internal (GtdSidebar  *self,
196
                  GtkListBox  *listbox,
197 198 199 200
                  GType        type,
                  GetDataFunc  get_data_func,
                  gpointer     data)
{
201
  GtkWidget *child;
202

203 204 205
  for (child = gtk_widget_get_first_child (GTK_WIDGET (listbox));
       child;
       child = gtk_widget_get_next_sibling (child))
206
    {
207 208
      if (g_type_is_a (G_OBJECT_TYPE (child), type) && get_data_func (child) == data)
          return child;
209 210 211 212 213 214 215 216 217 218
    }

  return NULL;
}

static GtkListBoxRow*
get_row_for_panel (GtdSidebar *self,
                   GtdPanel   *panel)
{
  return get_row_internal (self,
219
                           self->listbox,
220 221 222 223 224 225 226
                           GTD_TYPE_SIDEBAR_PANEL_ROW,
                           (GetDataFunc) gtd_sidebar_panel_row_get_panel,
                           panel);
}

static GtkListBoxRow*
get_row_for_provider (GtdSidebar  *self,
227
                      GtkListBox  *listbox,
228 229 230
                      GtdProvider *provider)
{
  return get_row_internal (self,
231
                           listbox,
232 233 234 235 236 237 238
                           GTD_TYPE_SIDEBAR_PROVIDER_ROW,
                           (GetDataFunc) gtd_sidebar_provider_row_get_provider,
                           provider);
}

static GtkListBoxRow*
get_row_for_task_list (GtdSidebar  *self,
239
                       GtkListBox  *listbox,
240 241 242
                       GtdTaskList *list)
{
  return get_row_internal (self,
243
                           listbox,
244 245 246 247 248
                           GTD_TYPE_SIDEBAR_LIST_ROW,
                           (GetDataFunc) gtd_sidebar_list_row_get_task_list,
                           list);
}

249 250 251 252 253 254 255 256 257
static void
activate_appropriate_row (GtdSidebar    *self,
                          GtkListBoxRow *row)
{
  GtkListBoxRow *to_be_activated;

  if (activate_row_below (self, GTD_SIDEBAR_LIST_ROW (row)))
    return;

258 259 260 261
  gtk_widget_activate_action (GTK_WIDGET (self),
                              "task-lists-workspace.toggle-archive",
                              "b",
                              FALSE);
262 263 264 265

  to_be_activated = gtk_list_box_get_row_at_index (self->listbox, 0);
  g_signal_emit_by_name (to_be_activated, "activate");
}
266 267 268 269 270

/*
 * Callbacks
 */

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
static void
on_action_move_up_activated_cb (GSimpleAction *simple,
                                GVariant      *parameters,
                                gpointer       user_data)
{
  GtkListBoxRow *selected_row;
  GtkListBoxRow *previous_row;
  GtdSidebar *self;
  gint selected_row_index;

  GTD_ENTRY;

  self = GTD_SIDEBAR (user_data);
  selected_row = gtk_list_box_get_selected_row (self->listbox);
  g_assert (selected_row != NULL);

  selected_row_index = gtk_list_box_row_get_index (selected_row);
  if (selected_row_index == 0)
    return;

  do
    {
      previous_row = gtk_list_box_get_row_at_index (self->listbox,
                                                    --selected_row_index);
    }
  while (previous_row &&
         (previous_row == self->archive_row ||
          !gtk_list_box_row_get_activatable (previous_row)));


  if (previous_row)
    g_signal_emit_by_name (previous_row, "activate");

  GTD_EXIT;
}

static void
on_action_move_down_activated_cb (GSimpleAction *simple,
                                  GVariant      *parameters,
                                  gpointer       user_data)
{
  GtkListBoxRow *selected_row;
  GtkListBoxRow *next_row;
  GtdSidebar *self;
  gint selected_row_index;

  GTD_ENTRY;

  self = GTD_SIDEBAR (user_data);
  selected_row = gtk_list_box_get_selected_row (self->listbox);
  g_assert (selected_row != NULL);

  selected_row_index = gtk_list_box_row_get_index (selected_row);

  do
    {
      next_row = gtk_list_box_get_row_at_index (self->listbox,
                                                ++selected_row_index);
    }
  while (next_row &&
         (next_row == self->archive_row ||
          !gtk_list_box_row_get_activatable (next_row)));


  if (next_row)
    g_signal_emit_by_name (next_row, "activate");

  GTD_EXIT;
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
static void
on_panel_added_cb (GtdManager *manager,
                   GtdPanel   *panel,
                   GtdSidebar *self)
{
  add_panel (self, panel);
}

static void
on_panel_removed_cb (GtdManager *manager,
                     GtdPanel   *panel,
                     GtdSidebar *self)
{
  GtkListBoxRow *row = get_row_for_panel (self, panel);

  g_debug ("Removing panel '%s'", gtd_panel_get_panel_name (panel));

  if (row)
359
    gtk_list_box_remove (self->listbox, GTK_WIDGET (row));
360 361
}

362 363 364 365 366 367 368 369 370 371
static void
on_provider_task_list_removed_cb (GObject      *source,
                                  GAsyncResult *result,
                                  gpointer      user_data)
{
  g_autoptr (GError) error = NULL;

  gtd_provider_remove_task_list_finish (GTD_PROVIDER (source), result, &error);
}

372 373 374 375 376 377 378 379 380 381 382 383 384
static void
delete_list_cb (GtdNotification *notification,
                gpointer         user_data)
{
  GtdTaskList *list;
  GtdProvider *provider;

  list = GTD_TASK_LIST (user_data);
  provider = gtd_task_list_get_provider (list);

  g_assert (provider != NULL);
  g_assert (gtd_task_list_is_removable (list));

385 386 387 388 389
  gtd_provider_remove_task_list (provider,
                                 list,
                                 NULL,
                                 on_provider_task_list_removed_cb,
                                 NULL);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
}

static void
undo_delete_list_cb (GtdNotification *notification,
                     gpointer         user_data)
{
  g_assert (GTD_IS_SIDEBAR_LIST_ROW (user_data));

  gtk_widget_show (GTK_WIDGET (user_data));
}

static void
on_task_list_panel_list_deleted_cb (GtdTaskListPanel *panel,
                                    GtdTaskList      *list,
                                    GtdSidebar       *self)
{
  GtdSidebarListRow *row;
  GtdNotification *notification;
  g_autofree gchar *title = NULL;

410 411 412 413 414
  if (gtd_task_list_get_archived (list))
    row = (GtdSidebarListRow*) get_row_for_task_list (self, self->archive_listbox, list);
  else
    row = (GtdSidebarListRow*) get_row_for_task_list (self, self->listbox, list);

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  g_assert (row != NULL && GTD_IS_SIDEBAR_LIST_ROW (row));

  GTD_TRACE_MSG ("Removing task list row from sidebar");

  title = g_strdup_printf (_("Task list <b>%s</b> removed"), gtd_task_list_get_name (list));
  notification = gtd_notification_new (title, 6000.0);
  gtd_notification_set_primary_action (notification, delete_list_cb, list);
  gtd_notification_set_secondary_action (notification, _("Undo"), undo_delete_list_cb, row);

  gtd_manager_send_notification (gtd_manager_get_default (), notification);

  /*
   * If the deleted list is selected, go to the next one (or previous, if
   * there are no other task list after this one).
   */
  if (gtk_list_box_row_is_selected (GTK_LIST_BOX_ROW (row)))
431
    activate_appropriate_row (self, GTK_LIST_BOX_ROW (row));
432 433 434 435

  gtk_widget_hide (GTK_WIDGET (row));
}

436 437 438 439 440 441 442 443 444
static void
on_listbox_row_activated_cb (GtkListBox    *panels_listbox,
                             GtkListBoxRow *row,
                             GtdSidebar    *self)
{
  if (GTD_IS_SIDEBAR_PANEL_ROW (row))
    {
      GtdPanel *panel = gtd_sidebar_panel_row_get_panel (GTD_SIDEBAR_PANEL_ROW (row));

445
      gtk_widget_activate_action (GTK_WIDGET (self),
446
                                  "task-lists-workspace.activate-panel",
447 448 449
                                  "(sv)",
                                  gtd_panel_get_panel_name (panel),
                                  g_variant_new_maybe (G_VARIANT_TYPE_VARIANT, NULL));
450 451 452 453 454 455 456
    }
  else if (GTD_IS_SIDEBAR_PROVIDER_ROW (row))
    {
      /* Do nothing */
    }
  else if (GTD_IS_SIDEBAR_LIST_ROW (row))
    {
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      GVariantBuilder builder;
      GtdProvider *provider;
      GtdTaskList *list;

      list = gtd_sidebar_list_row_get_task_list (GTD_SIDEBAR_LIST_ROW (row));
      provider = gtd_task_list_get_provider (list);

      g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
      g_variant_builder_add (&builder, "{sv}",
                             "provider-id",
                             g_variant_new_string (gtd_provider_get_id (provider)));
      g_variant_builder_add (&builder, "{sv}",
                             "task-list-id",
                             g_variant_new_string (gtd_object_get_uid (GTD_OBJECT (list))));

      gtk_widget_activate_action (GTK_WIDGET (self),
473
                                  "task-lists-workspace.activate-panel",
474 475 476
                                  "(sv)",
                                  "task-list-panel",
                                  g_variant_builder_end (&builder));
477
    }
478 479
  else if (row == self->archive_row)
    {
480 481 482 483
      gtk_widget_activate_action (GTK_WIDGET (self),
                                  "task-lists-workspace.toggle-archive",
                                  "b",
                                  TRUE);
484
    }
485 486 487 488 489 490 491 492 493 494 495 496
  else
    {
      g_assert_not_reached ();
    }
}

static void
on_panel_stack_visible_child_changed_cb (GtkStack   *panel_stack,
                                         GParamSpec *pspec,
                                         GtdSidebar *self)
{
  GtkListBoxRow *panel_row;
497
  GtkListBox *listbox;
498 499
  GtdPanel *visible_panel;

500 501
  GTD_ENTRY;

502 503 504
  g_assert (GTD_IS_PANEL (gtk_stack_get_visible_child (panel_stack)));

  visible_panel = GTD_PANEL (gtk_stack_get_visible_child (panel_stack));
505
  listbox = self->listbox;
506 507 508 509 510 511 512 513 514 515 516 517 518

  /*
   * If the currently visible panel is the tasklist panel, we
   * should choose the tasklist that is visible. Otherwise,
   * just select the panel.
   */
  if (visible_panel == self->task_list_panel)
    {
      GtdTaskList *task_list;

      task_list = gtd_task_list_panel_get_task_list (GTD_TASK_LIST_PANEL (self->task_list_panel));
      g_assert (task_list != NULL);

519 520 521 522 523 524 525
      panel_row = get_row_for_task_list (self, self->listbox, task_list);

      if (!panel_row)
        {
          panel_row = get_row_for_task_list (self, self->archive_listbox, task_list);
          listbox = self->archive_listbox;
        }
526 527 528 529 530 531 532
    }
  else
    {
      panel_row = get_row_for_panel (self, visible_panel);
    }

  /* Select the row if it's not already selected*/
533 534
  if (gtk_list_box_row_is_selected (panel_row))
    gtk_list_box_select_row (listbox, panel_row);
535 536

  GTD_EXIT;
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
}

static void
on_provider_added_cb (GtdManager  *manager,
                      GtdProvider *provider,
                      GtdSidebar  *self)
{
  add_provider (self, provider);
}

static void
on_provider_removed_cb (GtdManager  *manager,
                        GtdProvider *provider,
                        GtdSidebar  *self)
{
552
  GtkListBoxRow *row;
553 554 555

  g_debug ("Removing provider '%s'", gtd_provider_get_name (provider));

556
  row = get_row_for_provider (self, self->listbox, provider);
557
  gtk_list_box_remove (self->listbox, GTK_WIDGET (row));
558

559
  row = get_row_for_provider (self, self->archive_listbox, provider);
560
  gtk_list_box_remove (self->archive_listbox, GTK_WIDGET (row));
561 562 563 564 565 566 567 568 569 570 571
}


static void
on_task_list_added_cb (GtdManager  *manager,
                       GtdTaskList *list,
                       GtdSidebar  *self)
{
  add_task_list (self, list);
}

572 573 574 575 576 577
static void
on_task_list_changed_cb (GtdManager  *manager,
                         GtdTaskList *list,
                         GtdSidebar  *self)
{
  GtkListBoxRow *row;
578
  GtkListBox *listbox;
579 580 581
  gboolean archived;

  archived = gtd_task_list_get_archived (list);
582 583
  listbox = archived ? self->archive_listbox : self->listbox;
  row = get_row_for_task_list (self, listbox, list);
584 585 586 587 588 589 590

  /*
   * The task was either archived or unarchived; remove it and add to
   * the appropriate listbox.
   */
  if (!row)
    {
591 592
      listbox = archived ? self->listbox : self->archive_listbox;
      row = get_row_for_task_list (self, listbox, list);
593 594 595

      if (!row)
        goto out;
596 597 598 599 600 601

      /* Change to another panel or taklist */
      if (gtk_list_box_row_is_selected (row))
        activate_appropriate_row (self, row);

      /* Destroy the old row */
602
      gtk_list_box_remove (listbox, GTK_WIDGET (row));
603 604 605 606

      /* Add a new row */
      add_task_list (self, list);
    }
607

608
out:
609
  gtk_list_box_invalidate_filter (listbox);
610 611
}

612 613 614 615 616
static void
on_task_list_removed_cb (GtdManager  *manager,
                         GtdTaskList *list,
                         GtdSidebar  *self)
{
617
  GtkListBoxRow *row;
618
  GtkListBox *listbox;
619 620 621

  g_debug ("Removing task list '%s'", gtd_task_list_get_name (list));

622 623
  g_assert (!gtd_task_list_is_inbox (list));

624
  if (!gtd_task_list_get_archived (list))
625
    listbox = self->listbox;
626
  else
627
    listbox = self->archive_listbox;
628

629
  row = get_row_for_task_list (self, listbox, list);
630 631
  if (!row)
    return;
632

633
  gtk_widget_unparent (GTK_WIDGET (row));
634
  gtk_list_box_invalidate_filter (listbox);
635 636
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
static gboolean
filter_archive_listbox_cb (GtkListBoxRow *row,
                           gpointer       user_data)
{
  if (GTD_IS_SIDEBAR_LIST_ROW (row))
    {
      GtdTaskList *list;

      list = gtd_sidebar_list_row_get_task_list (GTD_SIDEBAR_LIST_ROW (row));
      return gtd_task_list_get_archived (list);
    }
  else if (GTD_IS_SIDEBAR_PROVIDER_ROW (row))
    {
      g_autoptr (GList) lists = NULL;
      GtdProvider *provider;
      GList *l;

      provider = gtd_sidebar_provider_row_get_provider (GTD_SIDEBAR_PROVIDER_ROW (row));
      lists = gtd_provider_get_task_lists (provider);

      for (l = lists; l; l = l->next)
        {
          if (gtd_task_list_get_archived (l->data))
            return TRUE;
        }

      return FALSE;
    }
  else
    {
      g_assert_not_reached ();
    }

  return FALSE;
}

673 674 675 676 677 678 679 680 681 682
static gboolean
filter_listbox_cb (GtkListBoxRow *row,
                   gpointer       user_data)
{
  GtdTaskList *list;

  if (!GTD_IS_SIDEBAR_LIST_ROW (row))
    return TRUE;

  list = gtd_sidebar_list_row_get_task_list (GTD_SIDEBAR_LIST_ROW (row));
683
  return !gtd_task_list_get_archived (list);
684 685
}

686 687 688 689 690
static gint
sort_listbox_cb (GtkListBoxRow *row_a,
                 GtkListBoxRow *row_b,
                 gpointer       user_data)
{
691 692 693 694 695 696 697 698 699 700 701
  GtdSidebar *self = GTD_SIDEBAR (user_data);

  /* Special-case the Archive row */
  if (row_a == self->archive_row || row_b == self->archive_row)
    {
      if (GTD_IS_SIDEBAR_PANEL_ROW (row_b))
        return 1;
      else
        return -1;
    }

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  if (G_OBJECT_TYPE (row_a) != G_OBJECT_TYPE (row_b))
    {
      gint result;

      /* Panels go above everything else */
      if (GTD_IS_SIDEBAR_PANEL_ROW (row_b) != GTD_IS_SIDEBAR_PANEL_ROW (row_a))
        return GTD_IS_SIDEBAR_PANEL_ROW (row_b) - GTD_IS_SIDEBAR_PANEL_ROW (row_a);

      /*
       * At this point, we know that row_a and row_b are either provider rows, or
       * tasklist rows. We also know that they're different, i.e. if row_a is a
       * provider row, row_b will be a list one, and vice-versa.
       */
      if (GTD_IS_SIDEBAR_PROVIDER_ROW (row_a))
        {
          GtdProvider *provider_a;
          GtdTaskList *list_b;

          provider_a = gtd_sidebar_provider_row_get_provider (GTD_SIDEBAR_PROVIDER_ROW (row_a));
          list_b = gtd_sidebar_list_row_get_task_list (GTD_SIDEBAR_LIST_ROW (row_b));

          /*
           * If the providers are different, respect the provider order. If the providers are the
           * same, we must put the provider row above the tasklist row.
           */
          result = gtd_provider_compare (provider_a, gtd_task_list_get_provider (list_b));

          if (result != 0)
            return result;

          return -1;
        }
      else
        {
          GtdTaskList *list_a;
          GtdProvider *provider_b;

          list_a = gtd_sidebar_list_row_get_task_list (GTD_SIDEBAR_LIST_ROW (row_a));
          provider_b = gtd_sidebar_provider_row_get_provider (GTD_SIDEBAR_PROVIDER_ROW (row_b));

          /* See comment above */
          result = gtd_provider_compare (gtd_task_list_get_provider (list_a), provider_b);

          if (result != 0)
            return result;

          return 1;
        }
    }
  else
    {
      /*
       * We only reach this section of the code if both rows are of the same type,
       * so it doesn't matter which one we get the type from.
       */

      if (GTD_IS_SIDEBAR_PANEL_ROW (row_a))
        return compare_panels (GTD_SIDEBAR_PANEL_ROW (row_a), GTD_SIDEBAR_PANEL_ROW (row_b));

      if (GTD_IS_SIDEBAR_PROVIDER_ROW (row_a))
        return compare_providers (GTD_SIDEBAR_PROVIDER_ROW (row_a), GTD_SIDEBAR_PROVIDER_ROW (row_b));

      if (GTD_IS_SIDEBAR_LIST_ROW (row_a))
        return compare_lists (GTD_SIDEBAR_LIST_ROW (row_a), GTD_SIDEBAR_LIST_ROW (row_b));
    }

  return 0;
}


/*
 * GObject overrides
 */

static void
gtd_sidebar_constructed (GObject *object)
{
  g_autoptr (GList) providers = NULL;
780
  GListModel *lists;
781 782 783
  GtdManager *manager;
  GtdSidebar *self;
  GList *l;
784
  guint i;
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

  self = (GtdSidebar *)object;
  manager = gtd_manager_get_default ();

  G_OBJECT_CLASS (gtd_sidebar_parent_class)->constructed (object);

  /* Add providers */
  providers = gtd_manager_get_providers (manager);

  for (l = providers; l; l = l->next)
    add_provider (self, l->data);

  g_signal_connect (manager, "provider-added", G_CALLBACK (on_provider_added_cb), self);
  g_signal_connect (manager, "provider-removed", G_CALLBACK (on_provider_removed_cb), self);

  /* Add task lists */
801
  lists = gtd_manager_get_task_lists_model (manager);
802

803 804 805 806 807 808
  for (i = 0; i < g_list_model_get_n_items (lists); i++)
    {
      g_autoptr (GtdTaskList) list = g_list_model_get_item (lists, i);

      add_task_list (self, list);
    }
809 810

  g_signal_connect (manager, "list-added", G_CALLBACK (on_task_list_added_cb), self);
811
  g_signal_connect (manager, "list-changed", G_CALLBACK (on_task_list_changed_cb), self);
812 813 814 815 816 817 818 819 820 821 822
  g_signal_connect (manager, "list-removed", G_CALLBACK (on_task_list_removed_cb), self);
}

static void
gtd_sidebar_class_init (GtdSidebarClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->constructed = gtd_sidebar_constructed;

823
  g_type_ensure (GTD_TYPE_MAX_SIZE_LAYOUT);
824

825
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/todo/plugins/task-lists-workspace/gtd-sidebar.ui");
826

827 828
  gtk_widget_class_bind_template_child (widget_class, GtdSidebar, archive_listbox);
  gtk_widget_class_bind_template_child (widget_class, GtdSidebar, archive_row);
829
  gtk_widget_class_bind_template_child (widget_class, GtdSidebar, listbox);
830 831
  gtk_widget_class_bind_template_child (widget_class, GtdSidebar, stack);

832 833 834 835 836 837 838 839
  gtk_widget_class_bind_template_callback (widget_class, on_listbox_row_activated_cb);

  gtk_widget_class_set_css_name (widget_class, "sidebar");
}

static void
gtd_sidebar_init (GtdSidebar *self)
{
840 841 842 843
  static const GActionEntry entries[] = {
    { "move-up", on_action_move_up_activated_cb },
    { "move-down", on_action_move_down_activated_cb },
  };
844 845 846
  gtk_widget_init_template (GTK_WIDGET (self));

  gtk_list_box_set_sort_func (self->listbox, sort_listbox_cb, self, NULL);
847 848
  gtk_list_box_set_filter_func (self->listbox, filter_listbox_cb, self, NULL);

849
  gtk_list_box_set_sort_func (self->archive_listbox, sort_listbox_cb, self, NULL);
850
  gtk_list_box_set_filter_func (self->archive_listbox, filter_archive_listbox_cb, self, NULL);
851 852 853 854 855 856 857 858 859 860 861

  self->action_group = g_simple_action_group_new ();

  g_action_map_add_action_entries (G_ACTION_MAP (self->action_group),
                                   entries,
                                   G_N_ELEMENTS (entries),
                                   self);

  gtk_widget_insert_action_group (GTK_WIDGET (self),
                                  "sidebar",
                                  G_ACTION_GROUP (self->action_group));
862 863 864 865 866 867 868 869 870
}

void
gtd_sidebar_set_panel_stack (GtdSidebar *self,
                             GtkStack   *stack)
{
  g_return_if_fail (GTD_IS_SIDEBAR (self));
  g_return_if_fail (GTK_IS_STACK (stack));

871
  g_assert (self->panel_stack == NULL);
872

873
  self->panel_stack = g_object_ref (stack);
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

  g_signal_connect_object (stack,
                           "notify::visible-child",
                           G_CALLBACK (on_panel_stack_visible_child_changed_cb),
                           self,
                           0);
}


void
gtd_sidebar_set_task_list_panel (GtdSidebar *self,
                                 GtdPanel   *task_list_panel)
{
  g_return_if_fail (GTD_IS_SIDEBAR (self));
  g_return_if_fail (GTD_IS_PANEL (task_list_panel));

  g_assert (self->task_list_panel == NULL);

  self->task_list_panel = g_object_ref (task_list_panel);
893 894 895 896 897
  g_signal_connect_object (self->task_list_panel,
                           "list-deleted",
                           G_CALLBACK (on_task_list_panel_list_deleted_cb),
                           self,
                           0);
898 899 900 901 902 903 904 905 906 907 908 909
}

void
gtd_sidebar_activate (GtdSidebar *self)
{
  GtkListBoxRow *first_row;

  g_assert (GTD_IS_SIDEBAR (self));

  first_row = gtk_list_box_get_row_at_index (self->listbox, 0);
  g_signal_emit_by_name (first_row, "activate");
}
910 911 912 913 914 915 916 917 918 919 920 921

void
gtd_sidebar_set_archive_visible (GtdSidebar *self,
                                 gboolean    show_archive)
{
  g_assert (GTD_IS_SIDEBAR (self));

  if (show_archive)
    gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->archive_listbox));
  else
    gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->listbox));
}
922 923 924 925 926 927 928 929

void
gtd_sidebar_connect (GtdSidebar *self,
                     GtkWidget  *workspace)
{
  g_signal_connect (workspace, "panel-added", G_CALLBACK (on_panel_added_cb), self);
  g_signal_connect (workspace, "panel-removed", G_CALLBACK (on_panel_removed_cb), self);
}