aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-folder-title-bar.h
blob: 092a465f144065911a109942c6c9fb9ebd6a08fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-shell-folder-title-bar.h
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * 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.
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

#ifndef __E_SHELL_FOLDER_TITLE_BAR_H__
#define __E_SHELL_FOLDER_TITLE_BAR_H__

#include <gtk/gtkeventbox.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#ifdef __cplusplus
extern "C" {
#pragma }
#endif /* __cplusplus */

#define E_TYPE_SHELL_FOLDER_TITLE_BAR           (e_shell_folder_title_bar_get_type ())
#define E_SHELL_FOLDER_TITLE_BAR(obj)           (GTK_CHECK_CAST ((obj), E_TYPE_SHELL_FOLDER_TITLE_BAR, EShellFolderTitleBar))
#define E_SHELL_FOLDER_TITLE_BAR_CLASS(klass)       (GTK_CHECK_CLASS_CAST ((klass), E_TYPE_SHELL_FOLDER_TITLE_BAR, EShellFolderTitleBarClass))
#define E_IS_SHELL_FOLDER_TITLE_BAR(obj)        (GTK_CHECK_TYPE ((obj), E_TYPE_SHELL_FOLDER_TITLE_BAR))
#define E_IS_SHELL_FOLDER_TITLE_BAR_CLASS(klass)    (GTK_CHECK_CLASS_TYPE ((obj), E_TYPE_SHELL_FOLDER_TITLE_BAR))


typedef struct _EShellFolderTitleBar        EShellFolderTitleBar;
typedef struct _EShellFolderTitleBarPrivate EShellFolderTitleBarPrivate;
typedef struct _EShellFolderTitleBarClass   EShellFolderTitleBarClass;

struct _EShellFolderTitleBar {
    GtkEventBox parent;

    EShellFolderTitleBarPrivate *priv;
};

struct _EShellFolderTitleBarClass {
    GtkEventBoxClass parent_class;

    /* Signals.  */
    void  (* title_toggled)  (EShellFolderTitleBar *folder_title_bar, gboolean pressed);
};


GtkType    e_shell_folder_title_bar_get_type          (void);
void       e_shell_folder_title_bar_construct         (EShellFolderTitleBar *folder_title_bar);
GtkWidget *e_shell_folder_title_bar_new               (void);

void       e_shell_folder_title_bar_set_title         (EShellFolderTitleBar *folder_title_bar,
                               const char           *title);
void       e_shell_folder_title_bar_set_folder_bar_label   (EShellFolderTitleBar *folder_title_bar,
                                const char           *folder_bar_label);
void       e_shell_folder_title_bar_set_icon          (EShellFolderTitleBar *folder_title_bar,
                               const GdkPixbuf      *icon);
void       e_shell_folder_title_bar_set_toggle_state  (EShellFolderTitleBar *folder_title_bar,
                               gboolean              state);
void       e_shell_folder_title_bar_set_clickable     (EShellFolderTitleBar *folder_title_bar,
                               gboolean              clickable);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __E_SHELL_FOLDER_TITLE_BAR_H__ */
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 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 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-local-storage.c
 *
 * Copyright (C) 2000, 2001 Ximian, Inc.
 *
 * 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.
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

/* FIXMEs:
 *
 *   - If we have `.' or `..' as path elements, we lose.
 *
 *   - If the LocalStorage is destroyed and an async operation on a shell component is
 *     pending, we get a callback on a bogus object.  We need support for cancelling
 *     operations on the shell component.
 *
 *   - The tree is kept both in the EStorage and the EvolutionStorage.  Very
 *     bad design.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <dirent.h>

#include <errno.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <gnome.h>

#include <bonobo.h>

#include <gal/util/e-util.h>
#include "e-local-folder.h"

#include "evolution-local-storage.h"

#include "e-local-storage.h"


#define PARENT_TYPE E_TYPE_STORAGE
static EStorageClass *parent_class = NULL;

#define SUBFOLDER_DIR_NAME     "subfolders"
#define SUBFOLDER_DIR_NAME_LEN 10

struct _ELocalStoragePrivate {
    EFolderTypeRegistry *folder_type_registry;
    char *base_path;
    EvolutionLocalStorage *bonobo_interface;
};


/* Utility functions.  */

/* Translate a storage path into a physical path on the file system.  */
static char *
get_physical_path (ELocalStorage *local_storage,
           const char *path)
{
    EStorage *storage;
    ELocalStoragePrivate *priv;
    const char *p, *newp;
    char *dp;
    char *real_path;
    int real_path_len;
    int base_path_len;

    storage = E_STORAGE (local_storage);
    priv = local_storage->priv;

    /* @path is always absolute, so it starts with a slash.  The base class should
           make sure this is the case; if not, it's broken.  */
    g_assert (*path == G_DIR_SEPARATOR);
    path++;

    /* Calculate the length of the real path. */

    real_path_len = strlen (path);
    real_path_len++;    /* For the ending zero.  */

    base_path_len = strlen (priv->base_path);
    real_path_len += base_path_len;
    real_path_len++;    /* For the separating slash.  */

    /* Take account for the fact that we need to translate every separator into
           `subfolders/'. */
    p = path;
    while (1) {
        newp = strchr (p, G_DIR_SEPARATOR);
        if (newp == NULL)
            break;

        real_path_len += SUBFOLDER_DIR_NAME_LEN;
        real_path_len++; /* For the separating slash.  */

        /* Skip consecutive slashes.  */
        while (*newp == G_DIR_SEPARATOR)
            newp++;

        p = newp;
    };

    real_path = g_malloc (real_path_len);
    dp = real_path;

    memcpy (dp, priv->base_path, base_path_len);
    dp += base_path_len;
    *(dp++) = G_DIR_SEPARATOR;

    /* Copy the mangled path.  */
    p = path;
    while (1) {
        newp = strchr (p, G_DIR_SEPARATOR);
        if (newp == NULL) {
            strcpy (dp, p);
            break;
        }

        memcpy (dp, p, newp - p + 1); /* `+ 1' to copy the slash too.  */
        dp += newp - p + 1;

        memcpy (dp, SUBFOLDER_DIR_NAME, SUBFOLDER_DIR_NAME_LEN);
        dp += SUBFOLDER_DIR_NAME_LEN;

        *(dp++) = G_DIR_SEPARATOR;

        /* Skip consecutive slashes.  */
        while (*newp == G_DIR_SEPARATOR)
            newp++;

        p = newp;
    }

    return real_path;
}

static void
new_folder (ELocalStorage *local_storage,
        const char *path,
        EFolder *folder)
{
    ELocalStoragePrivate *priv;

    priv = local_storage->priv;

    e_storage_new_folder (E_STORAGE (local_storage), path, folder);

    evolution_storage_new_folder (EVOLUTION_STORAGE (priv->bonobo_interface),
                      path,
                      e_folder_get_name (folder),
                      e_folder_get_type_string (folder),
                      e_folder_get_physical_uri (folder),
                      e_folder_get_description (folder),
                      e_folder_get_highlighted (folder));
}

static gboolean
load_folders (ELocalStorage *local_storage,
          const char *parent_path,
          const char *path,
          const char *physical_path)
{
    DIR *dir;
    char *subfolder_directory_path;

    if (parent_path == NULL) {
        /* On the top level, we don't have any folders and, consequently, no
           subfolder directory.  */

        subfolder_directory_path = g_strdup (physical_path);
    } else {
        EFolder *folder;

        /*  Otherwise, we have to load the corresponding folder.  */

        folder = e_local_folder_new_from_path (physical_path);
        if (folder == NULL)
            return FALSE;

        new_folder (local_storage, path, folder);

        subfolder_directory_path = g_concat_dir_and_file (physical_path, SUBFOLDER_DIR_NAME);
    }

    /* Now scan the subfolders and load them.  The subfolders are represented by
           directories under the "SUBFOLDER_DIR_NAME" directory.  */

    dir = opendir (subfolder_directory_path);

    if (dir == NULL) {
        g_free (subfolder_directory_path);
        return FALSE;
    }

    while (1) {
        struct stat file_stat;
        struct dirent *dirent;
        char *file_path;
        char *new_path;

        dirent = readdir (dir);
        if (dirent == NULL)
            break;

        if (strcmp (dirent->d_name, ".") == 0 || strcmp (dirent->d_name, "..") == 0)
            continue;

        file_path = g_concat_dir_and_file (subfolder_directory_path,
                           dirent->d_name);

        if (stat (file_path, &file_stat) < 0) {
            g_free (file_path);
            continue;
        }
        if (! S_ISDIR (file_stat.st_mode)) {
            g_free (file_path);
            continue;
        }

        new_path = g_concat_dir_and_file (path, dirent->d_name);

        load_folders (local_storage, path, new_path, file_path);

        g_free (file_path);
        g_free (new_path);
    }

    closedir (dir);
    g_free (subfolder_directory_path);

    return TRUE;
}

static gboolean
load_all_folders (ELocalStorage *local_storage)
{
    const char *base_path;

    base_path = e_local_storage_get_base_path (local_storage);

    return load_folders (local_storage, NULL, G_DIR_SEPARATOR_S, base_path);
}

static EStorageResult
errno_to_storage_result (void)
{
    EStorageResult storage_result;

    switch (errno) {
    case EACCES:
    case EROFS:
        storage_result = E_STORAGE_PERMISSIONDENIED;
        break;
    case EEXIST:
        storage_result = E_STORAGE_EXISTS;
        break;
    case ENOSPC:
        storage_result = E_STORAGE_NOSPACE;
        break;
    default:
        storage_result = E_STORAGE_GENERICERROR;
    }

    return storage_result;
}

static EStorageResult
shell_component_result_to_storage_result (EvolutionShellComponentResult result)
{
    /* FIXME: Maybe we need better mapping here.  */
    switch (result) {
    case EVOLUTION_SHELL_COMPONENT_OK:
        return E_STORAGE_OK;
    case EVOLUTION_SHELL_COMPONENT_NOTFOUND:
        return E_STORAGE_NOTFOUND;
    case EVOLUTION_SHELL_COMPONENT_UNSUPPORTEDTYPE:
        return E_STORAGE_UNSUPPORTEDTYPE;
    case EVOLUTION_SHELL_COMPONENT_UNSUPPORTEDOPERATION:
        return E_STORAGE_UNSUPPORTEDOPERATION;
    case EVOLUTION_SHELL_COMPONENT_EXISTS:
        return E_STORAGE_EXISTS;
    case EVOLUTION_SHELL_COMPONENT_PERMISSIONDENIED:
        return E_STORAGE_PERMISSIONDENIED;
    case EVOLUTION_SHELL_COMPONENT_ALREADYOWNED:
    case EVOLUTION_SHELL_COMPONENT_BUSY:
    case EVOLUTION_SHELL_COMPONENT_CORBAERROR:
    case EVOLUTION_SHELL_COMPONENT_HASSUBFOLDERS:
    case EVOLUTION_SHELL_COMPONENT_INTERNALERROR:
    case EVOLUTION_SHELL_COMPONENT_INTERRUPTED:
    case EVOLUTION_SHELL_COMPONENT_INVALIDARG:
    case EVOLUTION_SHELL_COMPONENT_INVALIDURI:
    case EVOLUTION_SHELL_COMPONENT_NOSPACE:
    case EVOLUTION_SHELL_COMPONENT_NOTOWNED:
    case EVOLUTION_SHELL_COMPONENT_UNKNOWNERROR:
    default:
        return E_STORAGE_GENERICERROR;
    }
}


/* Callbacks for the async methods invoked on the `Evolution::ShellComponent's.  */

struct _AsyncCreateFolderCallbackData {
    EStorage *storage;
    Bonobo_Listener listener;

    char *path;
    char *display_name;
    char *type;
    char *description;
    char *physical_uri;
    char *physical_path;

    EStorageResultCallback callback;
    void *callback_data;
};
typedef struct _AsyncCreateFolderCallbackData AsyncCreateFolderCallbackData;

static void
notify_listener (const Bonobo_Listener listener,
         EStorageResult result,
         const char *physical_path)
{
    CORBA_any any;
    GNOME_Evolution_Storage_FolderResult folder_result;
    CORBA_Environment ev;

    folder_result.result = result;
    folder_result.path = CORBA_string_dup (physical_path ? 
                           physical_path : "");
    any._type = TC_GNOME_Evolution_Storage_FolderResult;
    any._value = &folder_result;

    CORBA_exception_init (&ev);
    Bonobo_Listener_event (listener, "evolution-shell:folder_created", 
                   &any, &ev);
    CORBA_exception_free (&ev);
}

static void
component_async_create_folder_callback (EvolutionShellComponentClient *shell_component_client,
                    EvolutionShellComponentResult result,
                    void *data)
{
    AsyncCreateFolderCallbackData *callback_data;
    EStorageResult storage_result;

    callback_data = (AsyncCreateFolderCallbackData *) data;

    storage_result = shell_component_result_to_storage_result (result);

    if (result != EVOLUTION_SHELL_COMPONENT_OK) {
        /* XXX: This assumes the component won't leave any files in the directory.  */
        rmdir (callback_data->physical_path);
    } else {
        EFolder *folder;

        folder = e_local_folder_new (callback_data->display_name,
                         callback_data->type,
                         callback_data->description);

        e_folder_set_physical_uri (folder, callback_data->physical_uri);

        if (e_local_folder_save (E_LOCAL_FOLDER (folder))) {
            new_folder (E_LOCAL_STORAGE(callback_data->storage), callback_data->path, folder);
        } else {
            rmdir (callback_data->physical_path);
            gtk_object_unref (GTK_OBJECT (folder));
            storage_result = E_STORAGE_IOERROR;
        }
    }

    bonobo_object_unref (BONOBO_OBJECT (shell_component_client));

    if (callback_data->listener != CORBA_OBJECT_NIL)
        notify_listener (callback_data->listener, storage_result,
                 callback_data->physical_path);

    if (callback_data->callback != NULL)
        (* callback_data->callback) (callback_data->storage,
                         storage_result,
                         callback_data->callback_data);
    
    g_free (callback_data->path);
    g_free (callback_data->display_name);
    g_free (callback_data->type);
    g_free (callback_data->description);