aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-cell-checkbox.h
diff options
context:
space:
mode:
Diffstat (limited to 'e-util/e-cell-checkbox.h')
0 files changed, 0 insertions, 0 deletions
'n39' href='#n39'>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 80 81 82 83 84 85 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 132 133 134 135 136 137 138 139 140 141 142 143 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 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 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 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 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 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
/*
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *  Copyright (C) 2003, 2004 Christian Persch
 *  Copyright (C) 2004 Adam Hooper
 *
 *  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, 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.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ephy-extensions-manager.h"

#include "ephy-loader.h"
#include "ephy-shlib-loader.h"

#include "ephy-node-db.h"
#include "ephy-shell.h"
#include "eel-gconf-extensions.h"
#include "ephy-file-helpers.h"
#include "ephy-debug.h"

#include <libxml/xmlreader.h>

#include <gmodule.h>
#include <dirent.h>
#include <string.h>

#define CONF_LOADED_EXTENSIONS "/apps/epiphany/general/active_extensions"

#define EPHY_EXTENSIONS_MANAGER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_EXTENSIONS_MANAGER, EphyExtensionsManagerPrivate))

struct _EphyExtensionsManagerPrivate
{
    gboolean initialised;

    GList *data;
    GList *factories;
    GList *extensions;
    GList *windows;
    guint active_extensions_notifier_id;
};

typedef struct
{
    EphyExtensionInfo info;
    guint version;
    gboolean load_deferred;
    gboolean load_failed;

    xmlChar *gettext_domain;
    xmlChar *locale_directory;
    xmlChar *loader_type;
    GData *loader_attributes;

    EphyLoader *loader; /* NULL if never loaded */
    GObject *extension; /* NULL if unloaded */
} ExtensionInfo;

typedef struct
{
    char *type;
    EphyLoader *loader;
} LoaderInfo;

enum
{
    CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static GObjectClass *parent_class = NULL;

static void ephy_extensions_manager_class_init  (EphyExtensionsManagerClass *klass);
static void ephy_extensions_manager_iface_init  (EphyExtensionIface *iface);
static void ephy_extensions_manager_init    (EphyExtensionsManager *manager);

GType
ephy_extensions_manager_get_type (void)
{
    static GType type = 0;

    if (type == 0)
    {
        static const GTypeInfo our_info =
        {
            sizeof (EphyExtensionsManagerClass),
            NULL, /* base_init */
            NULL, /* base_finalize */
            (GClassInitFunc) ephy_extensions_manager_class_init,
            NULL,
            NULL, /* class_data */
            sizeof (EphyExtensionsManager),
            0, /* n_preallocs */
            (GInstanceInitFunc) ephy_extensions_manager_init
        };

        static const GInterfaceInfo extension_info =
        {
            (GInterfaceInitFunc) ephy_extensions_manager_iface_init,
            NULL,
            NULL
        };

        type = g_type_register_static (G_TYPE_OBJECT,
                           "EphyExtensionsManager",
                           &our_info, 0);

        g_type_add_interface_static (type,
                         EPHY_TYPE_EXTENSION,
                         &extension_info);
    }

    return type;
}

/**
 * ephy_extensions_manager_load:
 * @manager: an #EphyExtensionsManager
 * @name: identifier of the extension to load
 *
 * Loads the @name extension.
 **/
void
ephy_extensions_manager_load (EphyExtensionsManager *manager,
                  const char *identifier)
{
    GSList *gconf_exts;

    g_return_if_fail (EPHY_IS_EXTENSIONS_MANAGER (manager));
    g_return_if_fail (identifier != NULL);

    LOG ("Adding '%s' to extensions", identifier)

    gconf_exts = eel_gconf_get_string_list (CONF_LOADED_EXTENSIONS);

    if (!g_slist_find_custom (gconf_exts, identifier, (GCompareFunc) strcmp))
    {
        gconf_exts = g_slist_prepend (gconf_exts, g_strdup (identifier));

        eel_gconf_set_string_list (CONF_LOADED_EXTENSIONS, gconf_exts);
    }

    g_slist_foreach (gconf_exts, (GFunc) g_free, NULL);
    g_slist_free (gconf_exts);
}

/**
 * ephy_extensions_manager_unload:
 * @manager: an #EphyExtensionsManager
 * @name: filename of extension to unload, minus "lib" and "extension.so"
 *
 * Unloads the extension specified by @name.
 *
 * The extension with the same filename can afterwards be reloaded. However,
 * if any GTypes within the extension have changed parent types, Epiphany must
 * be restarted.
 **/
void
ephy_extensions_manager_unload (EphyExtensionsManager *manager,
                const char *identifier)
{
    GSList *gconf_exts;
    GSList *l;

    g_return_if_fail (EPHY_IS_EXTENSIONS_MANAGER (manager));
    g_return_if_fail (identifier != NULL);

    LOG ("Removing '%s' from extensions", identifier)

    gconf_exts = eel_gconf_get_string_list (CONF_LOADED_EXTENSIONS);

    l = g_slist_find_custom (gconf_exts, identifier, (GCompareFunc) strcmp);

    if (l != NULL)
    {
        gconf_exts = g_slist_remove_link (gconf_exts, l);
        g_free (l->data);
        g_slist_free_1 (l);

        eel_gconf_set_string_list (CONF_LOADED_EXTENSIONS, gconf_exts);
    }

    g_slist_foreach (gconf_exts, (GFunc) g_free, NULL);
    g_slist_free (gconf_exts);
}

/**
 * ephy_extensions_manager_register:
 * @manager: an #EphyExtensionsManager
 * @object: an Extension
 *
 * Registers @object with the extensions manager. @object must implement the
 * #EphyExtension interface.
 **/
void
ephy_extensions_manager_register (EphyExtensionsManager *manager,
                  GObject *object)
{
    g_return_if_fail (EPHY_IS_EXTENSIONS_MANAGER (manager));
    g_return_if_fail (EPHY_IS_EXTENSION (object));

    LOG ("Registering internal extension of type %s",
         g_type_name (((GTypeClass *) object)->g_type))

    manager->priv->extensions = g_list_prepend (manager->priv->extensions,
                            g_object_ref (object));
}


/**
 * ephy_extensions_manager_get_extensions:
 * @manager: an #EphyExtensionsManager
 *
 * Returns the list of known extensions.
 *
 * Returns: a list of #EphyExtensionInfo
 **/
GList *
ephy_extensions_manager_get_extensions (EphyExtensionsManager *manager)
{
    return g_list_copy (manager->priv->data);
}

static void
free_extension_info (ExtensionInfo *info)
{
    EphyExtensionInfo *einfo = (EphyExtensionInfo *) info;

    g_free (einfo->identifier);
    xmlFree ((xmlChar *) einfo->name);
    xmlFree ((xmlChar *) einfo->description);
    g_list_foreach (einfo->authors, (GFunc) xmlFree, NULL);
    g_list_free (einfo->authors);
    xmlFree ((xmlChar *) einfo->url);
    xmlFree ((xmlChar *) info->gettext_domain);
    xmlFree ((xmlChar *) info->locale_directory);
    xmlFree ((xmlChar *) info->loader_type);
    g_datalist_clear (&info->loader_attributes);

    if (info->extension != NULL)
    {
        g_return_if_fail (info->loader != NULL);

        ephy_loader_release_object (info->loader, info->extension);
    }
    if (info->loader != NULL)
    {
        g_object_unref (info->loader);
    }

    g_free (info);
}

static void
free_loader_info (LoaderInfo *info)
{
    g_free (info->type);
    g_object_unref (info->loader);
    g_free (info);
}

static int
find_extension_info (const ExtensionInfo *info,
             const char *identifier)
{
    return strcmp (info->info.identifier, identifier);
}

typedef enum
{
    STATE_START,
    STATE_STOP,
    STATE_ERROR,
    STATE_EXTENSION,
    STATE_NAME,
    STATE_DESCRIPTION,
    STATE_VERSION,
    STATE_AUTHOR,
    STATE_URL,
    STATE_GETTEXT_DOMAIN,
    STATE_LOCALE_DIRECTORY,
    STATE_LOADER,
    STATE_LOADER_ATTRIBUTE,
    STATE_LOAD_DEFERRED,
} ParserState;

static void
ephy_extensions_manager_load_file (EphyExtensionsManager *manager,
                   const char *dir,
                   const char *filename)
{
    xmlTextReaderPtr reader;
    ParserState state = STATE_START;
    GQuark attr_quark = 0;
    EphyExtensionInfo *einfo;
    ExtensionInfo *info;
    int ret;
    char *identifier, *dot, *path;

    LOG ("Loading description file '%s'", filename)

    identifier = g_path_get_basename (filename);
    dot = strstr (identifier, ".xml");
    g_return_if_fail (dot != NULL);
    *dot = '\0';

    if (g_list_find_custom (manager->priv->data, identifier,
                (GCompareFunc) find_extension_info) != NULL)
    {
        g_warning ("Extension description for '%s' already read!\n",
               identifier);
        g_free (identifier);
        return;
    }

    path = g_build_filename (dir, filename, NULL);
    if (g_file_test (path, G_FILE_TEST_EXISTS) == FALSE)
    {
        g_warning ("'%s' doesn't exist\n", filename);
        g_free (identifier);
        g_free (path);
        return;
    }
    
    reader = xmlNewTextReaderFilename (path);
    g_free (path);

    if (reader == NULL)
    {
        g_warning ("Couldn't read '%s'\n", filename);
        g_free (identifier);
        return;
    }

    info = g_new0 (ExtensionInfo, 1);
    einfo = (EphyExtensionInfo *) info;
    einfo->identifier = identifier;
    g_datalist_init (&info->loader_attributes);

    ret = xmlTextReaderRead (reader);

    while (ret == 1)
    {
        const xmlChar *tag;
        xmlReaderTypes type;

        tag = xmlTextReaderConstName (reader);
        type = xmlTextReaderNodeType (reader);

        if (state == STATE_LOADER &&
            type == XML_READER_TYPE_ELEMENT &&
            xmlStrEqual (tag, (const xmlChar *) "attribute"))
        {
            xmlChar *name;

            state = STATE_LOADER_ATTRIBUTE;

            name = xmlTextReaderGetAttribute (reader, (const xmlChar *) "name");
            attr_quark = g_quark_from_string ((const char *) name);
            xmlFree (name);
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "author"))
        {
            state = STATE_AUTHOR;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "description"))
        {
            state = STATE_DESCRIPTION;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "gettext-domain"))
        {
            state = STATE_GETTEXT_DOMAIN;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "load-deferred"))
        {
            state = STATE_LOAD_DEFERRED;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "locale-directory"))
        {
            state = STATE_LOCALE_DIRECTORY;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "name"))
        {
            state = STATE_NAME;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "url"))
        {
            state = STATE_URL;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "version"))
        {
            state = STATE_VERSION;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "loader"))
        {
            state = STATE_LOADER;

            info->loader_type = xmlTextReaderGetAttribute (reader, (const xmlChar *) "type");
        }
        else if (state == STATE_LOADER_ATTRIBUTE &&
             type == XML_READER_TYPE_TEXT &&
             attr_quark != 0)
        {
            xmlChar *value;

            value = xmlTextReaderValue (reader);

            g_datalist_id_set_data_full (&info->loader_attributes,
                             attr_quark, value,
                             (GDestroyNotify) xmlFree);
            attr_quark = 0;
        }
        else if (state == STATE_LOADER_ATTRIBUTE &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "attribute"))
        {
            state = STATE_LOADER;
        }
        else if (state == STATE_AUTHOR &&
             type == XML_READER_TYPE_TEXT)
        {
            einfo->authors = g_list_prepend
                (einfo->authors, xmlTextReaderValue (reader));
        }
        else if (state == STATE_DESCRIPTION &&
             type == XML_READER_TYPE_TEXT)
        {
            
            einfo->description = xmlTextReaderValue (reader);
        }
        else if (state == STATE_GETTEXT_DOMAIN &&
             type == XML_READER_TYPE_TEXT)
        {
            info->gettext_domain = xmlTextReaderValue (reader);
        }
        else if (state == STATE_LOAD_DEFERRED &&
             type == XML_READER_TYPE_TEXT)
        {
            const xmlChar *value;

            value = xmlTextReaderConstValue (reader);
            info->load_deferred =
                (value != NULL && xmlStrEqual (value, (const xmlChar *) "true"));
        }
        else if (state == STATE_LOCALE_DIRECTORY &&
             type == XML_READER_TYPE_TEXT)
        {
            info->locale_directory = xmlTextReaderValue (reader);
        }
        else if (state == STATE_NAME &&
             type == XML_READER_TYPE_TEXT)
        {
            einfo->name = xmlTextReaderValue (reader);
        }
        else if (state == STATE_VERSION &&
             type == XML_READER_TYPE_TEXT)
        {
            info->version = (guint) strtol ((const char *) xmlTextReaderConstValue (reader), NULL, 10);
        }
        else if (state == STATE_URL &&
             type == XML_READER_TYPE_TEXT)
        {
            einfo->url = xmlTextReaderValue (reader);
        }
        else if (state == STATE_AUTHOR &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "author"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_DESCRIPTION &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "description"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_GETTEXT_DOMAIN &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "gettext-domain"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_LOCALE_DIRECTORY &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "locale-directory"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_LOADER &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "loader"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_NAME &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "name"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_URL &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "url"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_VERSION &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "version"))
        {
            state = STATE_EXTENSION;
        }
        else if (type == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ||
             type == XML_READER_TYPE_WHITESPACE ||
             type == XML_READER_TYPE_TEXT)
        {
            /* eat it */
        }
        else if (state == STATE_START &&
             type == XML_READER_TYPE_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "extension"))
        {
            state = STATE_EXTENSION;
        }
        else if (state == STATE_EXTENSION &&
             type == XML_READER_TYPE_END_ELEMENT &&
             xmlStrEqual (tag, (const xmlChar *) "extension"))
        {
            state = STATE_STOP;
        }
        else
        {
            const xmlChar *content;

            content = xmlTextReaderConstValue (reader);
            g_warning ("tag '%s' of type %d in state %d with content '%s' was unexpected!",
                   tag, type, state, content ? (char *) content : "(null)");

            state = STATE_ERROR;
            break;
        }

        ret = xmlTextReaderRead (reader);
    }

    xmlFreeTextReader (reader);

    /* sanity check */
    if (ret < 0 || state != STATE_STOP ||
        einfo->name == NULL || einfo->description == NULL ||
        info->loader_type == NULL || info->loader_type[0] == '\0')
    {
        free_extension_info (info);
        return;
    }

    manager->priv->data = g_list_prepend (manager->priv->data, info);
}

static int
find_loader (const LoaderInfo *info,
          const char *type)
{
    return strcmp (info->type, type);
}

static EphyLoader *
get_loader_for_type (EphyExtensionsManager *manager,
              const char *type)
{
    LoaderInfo *info;
    GList *l;

    LOG ("Looking for loader for type '%s'", type)

    l = g_list_find_custom (manager->priv->factories, type,
                (GCompareFunc) find_loader);
    if (l != NULL)
    {
        info = (LoaderInfo *) l->data;
        return g_object_ref (info->loader);
    }

    if (strcmp (type, "shlib") == 0)
    {
        info = g_new (LoaderInfo, 1);
        info->type = g_strdup (type);
        info->loader = g_object_new (EPHY_TYPE_SHLIB_LOADER, NULL);

        manager->priv->factories =
            g_list_append (manager->priv->factories, info);

        return g_object_ref (info->loader);
    }

    /* try to load a loader */
    g_return_val_if_reached (NULL);

    return NULL;
}


static void
attach_window (EphyWindow *window,
           EphyExtension *extension)
{
    ephy_extension_attach_window (extension, window);
}

static void
load_extension (EphyExtensionsManager *manager,
        ExtensionInfo *info)
{

    EphyLoader *loader;

    g_return_if_fail (info->extension == NULL);

    LOG ("Loading extension '%s'", info->info.identifier)

    /* don't try again */
    if (info->load_failed) return;

    /* get a loader */
    loader = get_loader_for_type (manager, info->loader_type);
    if (loader == NULL)
    {
        g_warning ("No loader found for extension '%s' of type '%s'\n",
               info->info.identifier, info->loader_type);
        return;
    }

    info->loader = loader;

    info->extension = ephy_loader_get_object (loader,
                           &info->loader_attributes);

    if (info->extension != NULL)
    {
        manager->priv->extensions =
            g_list_prepend (manager->priv->extensions,
                    g_object_ref (info->extension));

        g_list_foreach (manager->priv->windows, (GFunc) attach_window,
                info->extension);

        info->info.active = TRUE;

        g_signal_emit (manager, signals[CHANGED], 0, info);
    }
    else
    {
        info->load_failed = TRUE;
    }
}

static void
detach_window (EphyWindow *window,
           EphyExtension *extension)
{
    ephy_extension_detach_window (extension, window);
}

static void
unload_extension (EphyExtensionsManager *manager,
          ExtensionInfo *info)
{
    g_return_if_fail (info->loader != NULL);
    g_return_if_fail (info->extension != NULL || info->load_failed);

    LOG ("Unloading extension '%s'", info->info.identifier)

    if (info->load_failed) return;

    g_list_foreach (manager->priv->windows, (GFunc) detach_window,
            info->extension);

    manager->priv->extensions =
        g_list_remove (manager->priv->extensions, info->extension);

    ephy_loader_release_object (info->loader, G_OBJECT (info->extension));
    g_object_unref (info->extension);

    info->info.active = FALSE;
    info->extension = NULL;

    g_signal_emit (manager, signals[CHANGED], 0, info);
}

static void
ephy_extensions_manager_load_dir (EphyExtensionsManager *manager,
                  const char *path)
{
    DIR *d;
    struct dirent *e;

    LOG ("Scanning directory '%s'", path)

    START_PROFILER ("Scanning directory")

    d = opendir (path);
    if (d == NULL)
    {
        return;
    }
    while ((e = readdir (d)) != NULL)
    {
        if (g_str_has_suffix (e->d_name, ".xml"))
        {
            ephy_extensions_manager_load_file (manager, path, e->d_name);
        }
    }
    closedir (d);

    STOP_PROFILER ("Scanning directory")
}

static void
active_extensions_notifier (GConfClient *client,
                guint cnxn_id,
                GConfEntry *entry,
                EphyExtensionsManager *manager)
{
    GSList *active_extensions;
    GList *l;
    gboolean active;
    ExtensionInfo *info;

    LOG ("Synching changed list of active extensions")

    active_extensions = eel_gconf_get_string_list (CONF_LOADED_EXTENSIONS);

    for (l = manager->priv->data; l != NULL; l = l->next)
    {
        info = (ExtensionInfo *) l->data;

        active = (g_slist_find_custom (active_extensions,
                           info->info.identifier,
                           (GCompareFunc) strcmp) != NULL);

        LOG ("Extension '%s' is %sactive and %sloaded",
             info->info.identifier,
             active ? "" : "not ",
             info->info.active ? "" : "not ")

        if (active != info->info.active)
        {
            if (active)
            {
                load_extension (manager, info);
            }
            else
            {
                unload_extension (manager, info);
            }
        }
    }

    g_slist_foreach (active_extensions, (GFunc) g_free, NULL);
    g_slist_free (active_extensions);
}

static void
ephy_extensions_manager_init (EphyExtensionsManager *manager)
{
    char *path;

    manager->priv = EPHY_EXTENSIONS_MANAGER_GET_PRIVATE (manager);

    LOG ("EphyExtensionsManager initialising")

    /* load the extensions descriptions */
    path = g_build_filename (ephy_dot_dir (), "extensions", NULL);
    ephy_extensions_manager_load_dir (manager, path);
    g_free (path);

    ephy_extensions_manager_load_dir (manager, EXTENSIONS_DIR);

    active_extensions_notifier (NULL, 0, NULL, manager);
    manager->priv->active_extensions_notifier_id =
        eel_gconf_notification_add
            (CONF_LOADED_EXTENSIONS,
             (GConfClientNotifyFunc) active_extensions_notifier,
             manager);
}

static void
ephy_extensions_manager_finalize (GObject *object)
{
    EphyExtensionsManager *manager = EPHY_EXTENSIONS_MANAGER (object);
    EphyExtensionsManagerPrivate *priv = manager->priv;

    LOG ("EphyExtensionsManager finalising")

    eel_gconf_notification_remove (manager->priv->active_extensions_notifier_id);

    g_list_foreach (priv->extensions, (GFunc) g_object_unref, NULL);
    g_list_free (priv->extensions);

    g_list_foreach (priv->factories, (GFunc) free_loader_info, NULL);
    g_list_free (priv->factories);

    g_list_foreach (priv->data, (GFunc) free_extension_info, NULL);
    g_list_free (priv->data);

    g_list_free (priv->windows);

    parent_class->finalize (object);
}

static void
impl_attach_window (EphyExtension *extension,
            EphyWindow *window)
{
    EphyExtensionsManager *manager = EPHY_EXTENSIONS_MANAGER (extension);

    LOG ("Attach")

    g_list_foreach (manager->priv->extensions,
            (GFunc) ephy_extension_attach_window, window);

    manager->priv->windows = g_list_prepend (manager->priv->windows, window);
}

static void
impl_detach_window (EphyExtension *extension,
            EphyWindow *window)
{
    EphyExtensionsManager *manager = EPHY_EXTENSIONS_MANAGER (extension);

    LOG ("Detach")

    manager->priv->windows = g_list_remove (manager->priv->windows, window);

    g_object_ref (window);

    g_list_foreach (manager->priv->extensions,
            (GFunc) ephy_extension_detach_window, window);

    g_object_unref (window);
}

static void
ephy_extensions_manager_iface_init (EphyExtensionIface *iface)
{
    iface->attach_window = impl_attach_window;
    iface->detach_window = impl_detach_window;
}

static void
ephy_extensions_manager_class_init (EphyExtensionsManagerClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    parent_class = (GObjectClass *) g_type_class_peek_parent (class);

    object_class->finalize = ephy_extensions_manager_finalize;

    signals[CHANGED] =
        g_signal_new ("changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EphyExtensionsManagerClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__POINTER,
                  G_TYPE_NONE,
                  1,
                  G_TYPE_POINTER);
    
    g_type_class_add_private (object_class, sizeof (EphyExtensionsManagerPrivate));
}