Back to index

unity  6.0.0
DesktopUtilities.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2012 Canonical Ltd
00004 *
00005 * This program is free software: you can redistribute it and/or modify
00006 * it under the terms of the GNU General Public License version 3 as
00007 * published by the Free Software Foundation.
00008 *
00009 * This program is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 * GNU General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU General Public License
00015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 *
00017 * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018 *              Andrea Azzarone <azzarone@gmail.com>
00019 */
00020 
00021 #include <algorithm>
00022 
00023 #include <glib.h>
00024 
00025 #include "DesktopUtilities.h"
00026 #include "GLibWrapper.h"
00027 
00028 namespace unity
00029 {
00030 
00031 std::string DesktopUtilities::GetUserDataDirectory()
00032 {
00033   const char* user_dir = g_get_user_data_dir();
00034 
00035   if (user_dir)
00036     return user_dir;
00037 
00038   // This shouldn't ever happen, but let's manually fallback to the default
00039   const char* home = g_get_home_dir();
00040 
00041   if (home)
00042   {
00043     const char* subdir = G_DIR_SEPARATOR_S ".local" G_DIR_SEPARATOR_S "share";
00044     return std::string(home).append(subdir);
00045   }
00046 
00047   return "";
00048 }
00049 
00050 std::vector<std::string> DesktopUtilities::GetSystemDataDirectories()
00051 {
00052   const char* const* system_dirs = g_get_system_data_dirs();
00053   std::vector<std::string> directories;
00054 
00055   for (unsigned int i = 0; system_dirs && system_dirs[i]; ++i)
00056   {
00057     if (system_dirs[i][0] != '\0')
00058     {
00059       std::string dir(system_dirs[i]);
00060       directories.push_back(dir);
00061     }
00062   }
00063 
00064   return directories;
00065 }
00066 
00067 std::vector<std::string> DesktopUtilities::GetDataDirectories()
00068 {
00069   std::vector<std::string> dirs = GetSystemDataDirectories();
00070   std::string const& user_directory = GetUserDataDirectory();
00071 
00072   dirs.push_back(user_directory);
00073 
00074   return dirs;
00075 }
00076 
00077 std::string DesktopUtilities::GetDesktopID(std::vector<std::string> const& default_paths,
00078                                            std::string const& desktop_path)
00079 {
00080   /* We check to see if the desktop file belongs to one of the system data
00081    * directories. If so, then we store it's desktop id, otherwise we store
00082    * it's full path. We're clever like that.
00083    */
00084 
00085   if (desktop_path.empty())
00086     return "";
00087 
00088   for (auto dir : default_paths)
00089   {
00090     if (!dir.empty())
00091     {
00092       if (dir.at(dir.length()-1) == G_DIR_SEPARATOR)
00093       {
00094         dir.append("applications" G_DIR_SEPARATOR_S);
00095       }
00096       else
00097       {
00098         dir.append(G_DIR_SEPARATOR_S "applications" G_DIR_SEPARATOR_S);
00099       }
00100 
00101       if (desktop_path.find(dir) == 0)
00102       {
00103         // if we are in a subdirectory of system path, the store name should
00104         // be subdir-filename.desktop
00105         std::string desktop_suffix = desktop_path.substr(dir.size());
00106         std::replace(desktop_suffix.begin(), desktop_suffix.end(), G_DIR_SEPARATOR, '-');
00107 
00108         return desktop_suffix;
00109       }
00110     }
00111   }
00112 
00113   return desktop_path;
00114 }
00115 
00116 std::string DesktopUtilities::GetDesktopID(std::string const& desktop_path)
00117 {
00118   std::vector<std::string> const& data_dirs = GetDataDirectories();
00119   return GetDesktopID(data_dirs, desktop_path);
00120 }
00121 
00122 
00123 std::string DesktopUtilities::GetBackgroundColor(std::string const& desktop_path)
00124 {
00125   GKeyFile* key_file = g_key_file_new();
00126 
00127   glib::Error error;
00128   g_key_file_load_from_file(key_file, desktop_path.c_str(), static_cast<GKeyFileFlags>(0), &error);
00129 
00130   if (error)
00131   {
00132     g_key_file_free(key_file);
00133     return "";
00134   }
00135 
00136   glib::String value(g_key_file_get_string(key_file, "Desktop Entry", "X-Unity-IconBackgroundColor", &error));
00137 
00138   g_key_file_free(key_file);
00139   return value.Str();
00140 }
00141 
00142 } // namespace unity