Back to index

bamf  0.2.120
Classes | Defines | Functions | Variables
bamf-factory.c File Reference
#include "bamf-factory.h"
#include "bamf-view.h"
#include "bamf-view-private.h"
#include "bamf-window.h"
#include "bamf-application.h"
#include "bamf-application-private.h"
#include "bamf-indicator.h"
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  _BamfFactoryPrivate

Defines

#define BAMF_FACTORY_GET_PRIVATE(o)   (G_TYPE_INSTANCE_GET_PRIVATE ((o), BAMF_TYPE_FACTORY, BamfFactoryPrivate))

Functions

 G_DEFINE_TYPE (BamfFactory, bamf_factory, G_TYPE_OBJECT)
 SECTION:bamf-factory : The base class for all factorys.
BamfApplication * bamf_application_new_favorite (const char *favorite_path)
BamfApplication * bamf_application_new (const char *path)
BamfWindow * bamf_window_new (const char *path)
BamfIndicator * bamf_indicator_new (const char *path)
static void bamf_factory_class_init (BamfFactoryClass *klass)
static void bamf_factory_init (BamfFactory *self)
static void on_view_closed (BamfView *view, BamfFactory *self)
static void on_view_weak_unref (BamfFactory *self, BamfView *view)
static void bamf_factory_register_view (BamfFactory *self, BamfView *view, const char *path)
BamfApplication * _bamf_factory_app_for_file (BamfFactory *factory, const char *path, gboolean create)
static BamfFactoryViewType compute_factory_type_by_str (const char *type)
BamfView * _bamf_factory_view_for_path (BamfFactory *factory, const char *path)
BamfView * _bamf_factory_view_for_path_type_str (BamfFactory *factory, const char *path, const char *type)
BamfView * _bamf_factory_view_for_path_type (BamfFactory *factory, const char *path, BamfFactoryViewType type)
BamfFactory * _bamf_factory_get_default (void)

Variables

static BamfFactory * factory = NULL

Class Documentation

struct _BamfFactoryPrivate

Definition at line 55 of file bamf-factory.c.

Class Members
GList * local_views
GList * registered_views
GHashTable * views

Define Documentation

#define BAMF_FACTORY_GET_PRIVATE (   o)    (G_TYPE_INSTANCE_GET_PRIVATE ((o), BAMF_TYPE_FACTORY, BamfFactoryPrivate))

Definition at line 52 of file bamf-factory.c.


Function Documentation

BamfApplication* _bamf_factory_app_for_file ( BamfFactory *  factory,
const char *  path,
gboolean  create 
)

Definition at line 131 of file bamf-factory.c.

{
  BamfApplication *result = NULL, *app;
  GList *l;
  
  /* check if result is available in known local_views */
  for (l = factory->priv->local_views; l; l = l->next)
    {
      if (!BAMF_IS_APPLICATION (l->data))
        continue;

      app = BAMF_APPLICATION (l->data);
      if (g_strcmp0 (bamf_application_get_desktop_file (app), path) == 0)
        {
          result = app;
          break;
        }
    }
  
  /* else create new */
  if (!result && create)
    {
      /* delay registration until match time */
      result = bamf_application_new_favorite (path);
      if (result)
        factory->priv->local_views = g_list_prepend (factory->priv->local_views, result);
    }
  
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

BamfFactory* _bamf_factory_get_default ( void  )

Definition at line 354 of file bamf-factory.c.

{
  
  if (BAMF_IS_FACTORY (factory))
    return factory;
  
  factory = (BamfFactory *) g_object_new (BAMF_TYPE_FACTORY, NULL);
  return factory;
}

Here is the caller graph for this function:

BamfView* _bamf_factory_view_for_path ( BamfFactory *  factory,
const char *  path 
)

Definition at line 193 of file bamf-factory.c.

Here is the call graph for this function:

Here is the caller graph for this function:

BamfView* _bamf_factory_view_for_path_type ( BamfFactory *  factory,
const char *  path,
BamfFactoryViewType  type 
)

Definition at line 209 of file bamf-factory.c.

{
  GHashTable *views;
  BamfView *view;
  GList *l;
  gboolean created = FALSE;

  g_return_val_if_fail (BAMF_IS_FACTORY (factory), NULL);
  
  if (!path || path[0] == '\0')
    return NULL;
  
  views = factory->priv->views;
  view = g_hash_table_lookup (views, path);
  
  if (BAMF_IS_VIEW (view))
    return view;

  if (type == BAMF_FACTORY_NONE)
    {
      view = g_object_new (BAMF_TYPE_VIEW, NULL);
      _bamf_view_set_path (view, path);
      type = compute_factory_type_by_str (bamf_view_get_view_type (view));
      g_object_unref (view);
      view = NULL;
    }

  switch (type)
  {
    case BAMF_FACTORY_VIEW:
      view = g_object_new (BAMF_TYPE_VIEW, NULL);
      break;
    case BAMF_FACTORY_WINDOW:
      view = BAMF_VIEW (bamf_window_new (path));
      break;
    case BAMF_FACTORY_APPLICATION:
      view = BAMF_VIEW (bamf_application_new (path));
      break;
    case BAMF_FACTORY_INDICATOR:
      view = BAMF_VIEW (bamf_indicator_new (path));
      break;
    case BAMF_FACTORY_NONE:
      view = NULL;
  }

  created = TRUE;
  BamfView *matched_view = NULL;

  if (BAMF_IS_APPLICATION (view))
    {
      /* handle case where a favorite exists and this matches it */
      const char *local_desktop_file = bamf_application_get_desktop_file (BAMF_APPLICATION (view));
      GList *local_children = bamf_view_get_children (view);

      for (l = factory->priv->local_views; l; l = l->next)
        {
          if (!BAMF_IS_APPLICATION (l->data))
            continue;

          BamfView *list_view = BAMF_VIEW (l->data);
          BamfApplication *list_app = BAMF_APPLICATION (l->data);

          const char *list_desktop_file = bamf_application_get_desktop_file (list_app);

          /* We try to match applications by desktop files */
          if (local_desktop_file && g_strcmp0 (local_desktop_file, list_desktop_file) == 0)
            {
              matched_view = list_view;
              break;
            }

          /* If the primary search doesn't give out any result, we fallback
           * to children window comparison */
          if (!matched_view)
            {
              GList *list_children, *ll;
              list_children = _bamf_application_get_cached_xids (list_app);

              for (ll = local_children; ll; ll = ll->next)
                {
                  if (!BAMF_IS_WINDOW (ll->data))
                    continue;

                  guint32 local_xid = bamf_window_get_xid (BAMF_WINDOW (ll->data));

                  if (g_list_find (list_children, GUINT_TO_POINTER (local_xid)))
                    {
                      matched_view = list_view;
                      break;
                    }
                }
            }
        }

      g_list_free (local_children);
    }
  else if (BAMF_IS_WINDOW (view))
    {
      guint32 local_xid = bamf_window_get_xid (BAMF_WINDOW (view));

      for (l = factory->priv->local_views; l; l = l->next)
        {
          if (!BAMF_IS_WINDOW (l->data))
            continue;

          BamfView *list_view = BAMF_VIEW (l->data);
          BamfWindow *list_win = BAMF_WINDOW (l->data);

          guint32 list_xid = bamf_window_get_xid (list_win);

          /* We try to match windows by xid */
          if (local_xid != 0 && local_xid == list_xid)
            {
              matched_view = list_view;
              break;
            }
        }
    }

  if (matched_view)
    {
      created = FALSE;
      g_object_unref (view);

      view = matched_view;
      _bamf_view_set_path (view, path);
      g_object_ref_sink (view);
    }

  if (view)
    {
      bamf_factory_register_view (factory, view, path);
      
      if (created)
        {
          factory->priv->local_views = g_list_prepend (factory->priv->local_views, view);
          g_object_ref_sink (view);
        }
    }

  return view;
}

Here is the call graph for this function:

Here is the caller graph for this function:

BamfView* _bamf_factory_view_for_path_type_str ( BamfFactory *  factory,
const char *  path,
const char *  type 
)

Definition at line 199 of file bamf-factory.c.

{
  g_return_val_if_fail (BAMF_IS_FACTORY (factory), NULL);
  BamfFactoryViewType factory_type = compute_factory_type_by_str (type);

  return _bamf_factory_view_for_path_type (factory, path, factory_type);
}

Here is the call graph for this function:

Here is the caller graph for this function:

BamfApplication* bamf_application_new ( const char *  path)

Definition at line 529 of file bamf-application.c.

{
  BamfApplication *self;
  self = g_object_new (BAMF_TYPE_APPLICATION, NULL);

  _bamf_view_set_path (BAMF_VIEW (self), path);

  return self;
}

Here is the call graph for this function:

Here is the caller graph for this function:

BamfApplication* bamf_application_new_favorite ( const char *  favorite_path)

Definition at line 540 of file bamf-application.c.

{
  BamfApplication *self;
  GKeyFile        *desktop_keyfile;
  GKeyFileFlags    flags;
  gchar           *type;
  gboolean         supported = FALSE;

  // check that we support this kind of desktop file
  desktop_keyfile = g_key_file_new ();
  flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
  if (g_key_file_load_from_file (desktop_keyfile, favorite_path, flags, NULL))
    {
      type = g_key_file_get_string (desktop_keyfile, "Desktop Entry", "Type", NULL);
      if (g_strcmp0 (type, "Application") == 0)
        supported = TRUE;

      g_key_file_free (desktop_keyfile);
      g_free (type);
    }
  if (!supported)
    return NULL;

  self = g_object_new (BAMF_TYPE_APPLICATION, NULL);

  self->priv->desktop_file = g_strdup (favorite_path);
  bamf_application_load_data_from_file (self);

  return self;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void bamf_factory_class_init ( BamfFactoryClass *  klass) [static]

Definition at line 73 of file bamf-factory.c.

{
  GObjectClass *obj_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (obj_class, sizeof (BamfFactoryPrivate));
}
static void bamf_factory_init ( BamfFactory *  self) [static]

Definition at line 82 of file bamf-factory.c.

{
  BamfFactoryPrivate *priv;

  priv = self->priv = BAMF_FACTORY_GET_PRIVATE (self);

  priv->views = g_hash_table_new_full ((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal, (GDestroyNotify) g_free, NULL);
}
static void bamf_factory_register_view ( BamfFactory *  self,
BamfView *  view,
const char *  path 
) [static]

Definition at line 114 of file bamf-factory.c.

{
  GHashTable *views;
  views = self->priv->views;
  
  g_hash_table_insert (views, g_strdup (path), view);

  if (g_list_find (self->priv->registered_views, view))
    return;
  
  self->priv->registered_views = g_list_prepend (self->priv->registered_views, view);
  
  g_signal_connect (G_OBJECT (view), "closed", (GCallback) on_view_closed, self);
  g_object_weak_ref (G_OBJECT (view), (GWeakNotify) on_view_weak_unref, self);
}

Here is the call graph for this function:

Here is the caller graph for this function:

BamfIndicator* bamf_indicator_new ( const char *  path)

Definition at line 265 of file bamf-indicator.c.

{
  BamfIndicator *self;
  self = g_object_new (BAMF_TYPE_INDICATOR, NULL);

  _bamf_view_set_path (BAMF_VIEW (self), path);

  return self;
}

Here is the call graph for this function:

Here is the caller graph for this function:

BamfWindow* bamf_window_new ( const char *  path)

Definition at line 475 of file bamf-window.c.

{
  BamfWindow *self;
  self = g_object_new (BAMF_TYPE_WINDOW, NULL);

  _bamf_view_set_path (BAMF_VIEW (self), path);

  return self;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static BamfFactoryViewType compute_factory_type_by_str ( const char *  type) [static]

Definition at line 165 of file bamf-factory.c.

{
  BamfFactoryViewType factory_type = BAMF_FACTORY_NONE;

  if (type && type[0] != '\0')
    {
      if (g_strcmp0 (type, "window") == 0)
        {
          factory_type = BAMF_FACTORY_WINDOW;
        }
      else if (g_strcmp0 (type, "application") == 0)
        {
          factory_type = BAMF_FACTORY_APPLICATION;
        }
      else if (g_strcmp0 (type, "indicator") == 0)
        {
          factory_type = BAMF_FACTORY_INDICATOR;
        }
      else if (g_strcmp0 (type, "view") == 0)
        {
          factory_type = BAMF_FACTORY_VIEW;
        }
    }

  return factory_type;
}

Here is the caller graph for this function:

G_DEFINE_TYPE ( BamfFactory  ,
bamf_factory  ,
G_TYPE_OBJECT   
)

SECTION:bamf-factory : The base class for all factorys.

BamfFactory is the base class that all factorys need to derive from.

static void on_view_closed ( BamfView *  view,
BamfFactory *  self 
) [static]

Definition at line 92 of file bamf-factory.c.

{
  const char *path;
  
  g_return_if_fail (BAMF_IS_VIEW (view));
  
  path = _bamf_view_get_path (view);
  if (path)
    g_hash_table_remove (self->priv->views, path);
  
  g_object_unref (view);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void on_view_weak_unref ( BamfFactory *  self,
BamfView *  view 
) [static]

Definition at line 106 of file bamf-factory.c.

{
  g_return_if_fail (BAMF_IS_FACTORY (self));
  self->priv->local_views = g_list_remove (self->priv->local_views, view);
  self->priv->registered_views = g_list_remove (self->priv->registered_views, view);
}

Here is the caller graph for this function:


Variable Documentation

BamfFactory* factory = NULL [static]

Definition at line 62 of file bamf-factory.c.