summaryrefslogtreecommitdiff
diff options
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/FavoriteStore.h2
-rw-r--r--src/FavoriteStoreGSettings.cpp88
-rw-r--r--src/FavoriteStoreGSettings.h2
-rw-r--r--tests/CMakeLists.txt6
-rw-r--r--tests/data/update-manager.desktop10
-rw-r--r--tests/unit/TestFavoriteStoreGSettings.cpp106
7 files changed, 189 insertions, 27 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index aaefa8d2e..dbdc43294 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74,7 +74,7 @@ endif (${GETTEXT_FOUND} STREQUAL "TRUE")
#
# src (Compiz Plugin)
#
-set (UNITY_PLUGIN_DEPS "nux-0.9;libbamf;dbus-glib-1;dee-1.0;gio-2.0;gio-unix-2.0;dbusmenu-glib;x11")
+set (UNITY_PLUGIN_DEPS "nux-0.9;libbamf;dbus-glib-1;dee-1.0;gio-2.0;gio-unix-2.0;dbusmenu-glib;x11;gthread-2.0")
find_package (Compiz REQUIRED)
include (CompizPlugin)
diff --git a/src/FavoriteStore.h b/src/FavoriteStore.h
index 2c173985f..b12e9ff27 100644
--- a/src/FavoriteStore.h
+++ b/src/FavoriteStore.h
@@ -47,7 +47,7 @@ public:
virtual GSList * GetFavorites () = 0;
// These will emit the relevant signals, so bare that in mind
- virtual void AddFavorite (const char *desktop_path, guint32 position) = 0;
+ virtual void AddFavorite (const char *desktop_path, gint position) = 0;
virtual void RemoveFavorite (const char *desktop_path) = 0;
virtual void MoveFavorite (const char *desktop_path, guint32 position) = 0;
diff --git a/src/FavoriteStoreGSettings.cpp b/src/FavoriteStoreGSettings.cpp
index 0124ae261..a4310964c 100644
--- a/src/FavoriteStoreGSettings.cpp
+++ b/src/FavoriteStoreGSettings.cpp
@@ -20,9 +20,11 @@
#include "FavoriteStoreGSettings.h"
+#define SETTINGS_NAME "com.canonical.Unity.Launcher"
+
FavoriteStoreGSettings::FavoriteStoreGSettings ()
{
- m_settings = g_settings_new ("com.canonical.Unity.Launcher");
+ m_settings = g_settings_new (SETTINGS_NAME);
m_favorites = NULL;
Refresh ();
@@ -30,7 +32,7 @@ FavoriteStoreGSettings::FavoriteStoreGSettings ()
FavoriteStoreGSettings::FavoriteStoreGSettings (GSettingsBackend *backend)
{
- m_settings = g_settings_new_with_backend ("com.canonical.Unity.Launcher", backend);
+ m_settings = g_settings_new_with_backend (SETTINGS_NAME, backend);
m_favorites = NULL;
Refresh ();
@@ -52,27 +54,46 @@ FavoriteStoreGSettings::Refresh ()
g_slist_foreach (m_favorites, (GFunc)g_free, NULL);
g_slist_free (m_favorites);
+ m_favorites = NULL;
favs = g_settings_get_strv (m_settings, "favorites");
while (favs[i] != NULL)
{
- GDesktopAppInfo *info;
-
- info = g_desktop_app_info_new (favs[i]);
-
- if (info == NULL || g_desktop_app_info_get_filename (info) == NULL)
+ /*
+ * We will be storing either full /path/to/desktop/files or foo.desktop id's
+ */
+ if (favs[i][0] == '/')
{
- g_warning ("Unable to load GDesktopAppInfo for '%s'", favs[i]);
+ if (g_file_test (favs[i], G_FILE_TEST_EXISTS))
+ {
+ m_favorites = g_slist_append (m_favorites, g_strdup (favs[i]));
+ }
+ else
+ {
+ g_warning ("Unable to load desktop file: %s", favs[i]);
+ }
+ }
+ else
+ {
+ GDesktopAppInfo *info;
- i++;
- continue;
+ info = g_desktop_app_info_new (favs[i]);
+
+ if (info == NULL || g_desktop_app_info_get_filename (info) == NULL)
+ {
+ g_warning ("Unable to load GDesktopAppInfo for '%s'", favs[i]);
+
+ i++;
+ continue;
+ }
+
+ m_favorites = g_slist_append (m_favorites, g_strdup (g_desktop_app_info_get_filename (info)));
+
+ g_object_unref (info);
}
- m_favorites = g_slist_append (m_favorites, g_strdup (g_desktop_app_info_get_filename (info)));
i++;
-
- g_object_unref (info);
}
g_strfreev (favs);
@@ -84,13 +105,38 @@ FavoriteStoreGSettings::GetFavorites ()
return m_favorites;
}
+static gchar *
+get_basename_or_path (const gchar *desktop_path)
+{
+ const gchar * const * dirs;
+ const gchar * dir;
+ gint i = 0;
+
+ dirs = g_get_system_data_dirs ();
+
+ /* We check to see if the desktop file belongs to one of the system data
+ * directories. If so, then we store it's desktop id, otherwise we store
+ * it's full path. We're clever like that.
+ */
+ while ((dir = dirs[i]))
+ {
+ if (g_str_has_prefix (desktop_path, dir))
+ {
+ return g_path_get_basename (desktop_path);
+ }
+ i++;
+ }
+
+ return g_strdup (desktop_path);
+}
+
void
FavoriteStoreGSettings::AddFavorite (const char *desktop_path,
- guint32 position)
+ gint position)
{
int n_total_favs;
GSList *f;
- guint32 i = 0;
+ gint i = 0;
g_return_if_fail (desktop_path);
@@ -103,17 +149,23 @@ FavoriteStoreGSettings::AddFavorite (const char *desktop_path,
{
if (i == position)
{
- favs[i] = g_path_get_basename (desktop_path);
+ favs[i] = get_basename_or_path (desktop_path);
i++;
}
- favs[i] = g_path_get_basename ((char *)f->data);
+ favs[i] = get_basename_or_path ((char *)f->data);
i++;
}
+ /* Add it to the end of the list */
+ if (position == -1)
+ {
+ favs[i] = get_basename_or_path (desktop_path);
+ }
+
if (!g_settings_set_strv (m_settings, "favorites", favs))
- g_warning ("Unable to add a new favorite");
+ g_warning ("Unable to add a new favorite '%s' at position '%u'", desktop_path, position);
i = 0;
while (favs[i] != NULL)
diff --git a/src/FavoriteStoreGSettings.h b/src/FavoriteStoreGSettings.h
index 0c25b4d2f..016a16ddc 100644
--- a/src/FavoriteStoreGSettings.h
+++ b/src/FavoriteStoreGSettings.h
@@ -36,7 +36,7 @@ public:
//Methods
virtual GSList * GetFavorites ();
- virtual void AddFavorite (const char *desktop_path, guint32 position);
+ virtual void AddFavorite (const char *desktop_path, gint position);
virtual void RemoveFavorite (const char *desktop_path);
virtual void MoveFavorite (const char *desktop_path, guint32 position);
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 140e9d83a..1b5218b18 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,4 +1,10 @@
#
+# Data
+#
+configure_file (${CMAKE_CURRENT_SOURCE_DIR}/data/update-manager.desktop
+ ${CMAKE_BINARY_DIR}/tests/data/update-manager.desktop)
+
+#
# Unit tests
#
find_package (PkgConfig)
diff --git a/tests/data/update-manager.desktop b/tests/data/update-manager.desktop
new file mode 100644
index 000000000..5f9f3a64f
--- /dev/null
+++ b/tests/data/update-manager.desktop
@@ -0,0 +1,10 @@
+[Desktop Entry]
+Name=Update Manager
+GenericName=Software Updates
+Comment=Show and install available updates
+Exec=/usr/bin/update-manager
+Icon=update-manager
+Terminal=false
+Type=Application
+Categories=System;Settings;
+X-Ubuntu-Gettext-Domain=update-manager
diff --git a/tests/unit/TestFavoriteStoreGSettings.cpp b/tests/unit/TestFavoriteStoreGSettings.cpp
index e10efec14..0597d4b2b 100644
--- a/tests/unit/TestFavoriteStoreGSettings.cpp
+++ b/tests/unit/TestFavoriteStoreGSettings.cpp
@@ -28,15 +28,23 @@
#include "FavoriteStore.h"
#include "FavoriteStoreGSettings.h"
+#define CUSTOM_DESKTOP BUILDDIR"/tests/data/update-manager.desktop"
+
#define BASE_STORE_FILE BUILDDIR"/settings/test-favorite-store-gsettings.store"
#define BASE_STORE_CONTENTS "[desktop/unity/launcher]\n" \
- "favorites=['evolution.desktop', 'firefox.desktop']"
+ "favorites=['evolution.desktop', 'firefox.desktop', '%s']"
-static const char *base_store_favs[] = { "evolution.desktop", "firefox.desktop", NULL };
-static int n_base_store_favs = 2;
+static const char *base_store_favs[] = { "evolution.desktop",
+ "firefox.desktop",
+ CUSTOM_DESKTOP,
+ NULL };
+static int n_base_store_favs = G_N_ELEMENTS (base_store_favs) - 1; /* NULL */
static void TestAllocation (void);
static void TestGetFavorites (void);
+static void TestAddFavorite (void);
+static void TestAddFavoritePosition (void);
+static void TestAddFavoriteLast (void);
void
TestFavoriteStoreGSettingsCreateSuite ()
@@ -45,6 +53,9 @@ TestFavoriteStoreGSettingsCreateSuite ()
g_test_add_func (_DOMAIN"/Allocation", TestAllocation);
g_test_add_func (_DOMAIN"/GetFavorites", TestGetFavorites);
+ g_test_add_func (_DOMAIN"/AddFavorite", TestAddFavorite);
+ g_test_add_func (_DOMAIN"/AddFavoritePosition", TestAddFavoritePosition);
+ g_test_add_func (_DOMAIN"/AddFavoriteLast", TestAddFavoriteLast);
}
static GSettingsBackend *
@@ -52,23 +63,34 @@ CreateDefaultKeyFileBackend ()
{
GSettingsBackend *b;
GError *error = NULL;
+ gchar *contents = NULL;
+
+ contents = g_strdup_printf (BASE_STORE_CONTENTS, CUSTOM_DESKTOP);
g_file_set_contents (BASE_STORE_FILE,
- BASE_STORE_CONTENTS,
+ contents,
-1,
&error);
g_assert (error == NULL);
b = g_keyfile_settings_backend_new (BASE_STORE_FILE, "/", "root");
+ g_free (contents);
return b;
}
static void
TestAllocation ()
{
- FavoriteStoreGSettings *settings = new FavoriteStoreGSettings ();
+ GSettingsBackend *backend;
+ FavoriteStoreGSettings *settings;
+
+ backend = CreateDefaultKeyFileBackend ();
+ g_assert (G_IS_SETTINGS_BACKEND (backend));
+
+ settings = new FavoriteStoreGSettings (backend);
g_assert (settings != NULL);
+ g_object_unref (backend);
settings->UnReference ();
}
@@ -97,10 +119,82 @@ TestGetFavorites ()
gchar *basename;
basename = g_path_get_basename ((char*)f->data);
- g_assert_cmpstr (basename, ==, base_store_favs[i]);
+ if (g_strcmp0 (basename, "update-manager.desktop") == 0)
+ g_assert_cmpstr (CUSTOM_DESKTOP, ==, base_store_favs[i]);
+ else
+ g_assert_cmpstr (basename, ==, base_store_favs[i]);
g_free (basename);
}
settings->UnReference ();
}
+
+static void
+TestAddFavorite ()
+{
+#define OTHER_DESKTOP "/usr/share/applications/nautilus.desktop"
+ GSettingsBackend *backend;
+ FavoriteStoreGSettings *settings;
+ GSList *favs;
+
+ backend = CreateDefaultKeyFileBackend ();
+ g_assert (G_IS_SETTINGS_BACKEND (backend));
+
+ settings = new FavoriteStoreGSettings (backend);
+ g_assert (settings != NULL);
+ g_object_unref (backend);
+
+ settings->AddFavorite (OTHER_DESKTOP, 0);
+
+ favs = settings->GetFavorites ();
+ g_assert_cmpstr ((const gchar *)g_slist_nth_data (favs, 0), ==, OTHER_DESKTOP);
+
+ settings->UnReference ();
+}
+
+static void
+TestAddFavoritePosition ()
+{
+#define OTHER_DESKTOP "/usr/share/applications/nautilus.desktop"
+ GSettingsBackend *backend;
+ FavoriteStoreGSettings *settings;
+ GSList *favs;
+
+ backend = CreateDefaultKeyFileBackend ();
+ g_assert (G_IS_SETTINGS_BACKEND (backend));
+
+ settings = new FavoriteStoreGSettings (backend);
+ g_assert (settings != NULL);
+ g_object_unref (backend);
+
+ settings->AddFavorite (OTHER_DESKTOP, 2);
+
+ favs = settings->GetFavorites ();
+ g_assert_cmpstr ((const gchar *)g_slist_nth_data (favs, 2), ==, OTHER_DESKTOP);
+
+ settings->UnReference ();
+}
+
+static void
+TestAddFavoriteLast ()
+{
+#define OTHER_DESKTOP "/usr/share/applications/nautilus.desktop"
+ GSettingsBackend *backend;
+ FavoriteStoreGSettings *settings;
+ GSList *favs;
+
+ backend = CreateDefaultKeyFileBackend ();
+ g_assert (G_IS_SETTINGS_BACKEND (backend));
+
+ settings = new FavoriteStoreGSettings (backend);
+ g_assert (settings != NULL);
+ g_object_unref (backend);
+
+ settings->AddFavorite (OTHER_DESKTOP, -1);
+
+ favs = settings->GetFavorites ();
+ g_assert_cmpstr ((const gchar *)g_slist_nth_data (favs, n_base_store_favs), ==, OTHER_DESKTOP);
+
+ settings->UnReference ();
+}