Back to index

unity  6.0.0
DevicesSettings.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010 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: Andrea Azzarone <aazzarone@hotmail.it>
00018  */
00019 
00020 #include "DevicesSettings.h"
00021 
00022 #include <algorithm>
00023 
00024 namespace unity {
00025 
00026 namespace {
00027 
00028 const char* SETTINGS_NAME = "com.canonical.Unity.Devices";
00029 
00030 void on_settings_updated(GSettings* settings,
00031                          const gchar* key,
00032                          DevicesSettings* self);
00033 
00034 } // anonymous namespace
00035 
00036 DevicesSettings& DevicesSettings::GetDefault()
00037 {
00038   static DevicesSettings instance;
00039   return instance;
00040 }
00041 
00042 DevicesSettings::DevicesSettings()
00043   : settings_(g_settings_new(SETTINGS_NAME))
00044   , ignore_signals_(false)
00045   , devices_option_(ONLY_MOUNTED)
00046 {
00047 
00048   g_signal_connect(settings_, "changed", G_CALLBACK(on_settings_updated), this);
00049 
00050   Refresh();
00051 }
00052 
00053 void DevicesSettings::Refresh()
00054 {
00055   gchar** favs = g_settings_get_strv(settings_, "favorites");
00056 
00057   favorites_.clear();
00058 
00059   for (int i = 0; favs[i] != NULL; i++)
00060     favorites_.push_back(favs[i]);
00061 
00062   g_strfreev(favs);
00063 }
00064 
00065 void DevicesSettings::AddFavorite(std::string const& uuid)
00066 {
00067   if (uuid.empty())
00068     return;
00069   
00070   favorites_.push_back(uuid);
00071 
00072   SaveFavorites(favorites_);
00073   Refresh();
00074 }
00075 
00076 void DevicesSettings::RemoveFavorite(std::string const& uuid)
00077 {
00078   if (uuid.empty())
00079     return;
00080 
00081   DeviceList::iterator pos = std::find(favorites_.begin(), favorites_.end(), uuid);
00082   if (pos == favorites_.end())
00083     return;
00084 
00085   favorites_.erase(pos);
00086   SaveFavorites(favorites_);
00087   Refresh();
00088 }
00089 
00090 void DevicesSettings::SaveFavorites(DeviceList const& favorites)
00091 {
00092   const int size = favorites.size();
00093   const char* favs[size + 1];
00094   favs[size] = NULL;
00095 
00096   int index = 0;
00097   for (DeviceList::const_iterator i = favorites.begin(), end = favorites.end();
00098        i != end; ++i, ++index)
00099   {
00100     favs[index] = i->c_str();
00101   }
00102 
00103   ignore_signals_ = true;
00104   if (!g_settings_set_strv(settings_, "favorites", favs))
00105     g_warning("Saving favorites failed.");
00106   ignore_signals_ = false;
00107 }
00108 
00109 
00110 void DevicesSettings::Changed(std::string const& key)
00111 {
00112   if (ignore_signals_)
00113     return;
00114   
00115   Refresh();
00116   
00117   changed.emit();
00118 }
00119 
00120 void DevicesSettings::SetDevicesOption(DevicesOption devices_option)
00121 {
00122   if (devices_option == devices_option_)
00123     return;
00124 
00125   devices_option_ = devices_option;
00126 
00127   changed.emit();
00128 }
00129 
00130 namespace {
00131 
00132 void on_settings_updated(GSettings* settings,
00133                          const gchar* key,
00134                          DevicesSettings* self)
00135 {
00136   if (settings and key) {
00137     self->Changed(key);
00138   }
00139 }
00140 
00141 } // anonymous namespace
00142 
00143 } // namespace unity