1
1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
3
* Copyright (C) 2010 Canonical Ltd
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 3 as
7
* published by the Free Software Foundation.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Authored by: Andrea Azzarone <aazzarone@hotmail.it>
3
* Copyright (C) 2010 Canonical Ltd
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 3 as
7
* published by the Free Software Foundation.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Authored by: Andrea Azzarone <aazzarone@hotmail.it>
20
20
#include "DevicesSettings.h"
22
static DevicesSettings* _devices_settings = NULL;
28
const char* SETTINGS_NAME = "com.canonical.Unity.Devices";
30
void on_settings_updated(GSettings* settings,
32
DevicesSettings* self);
34
} // anonymous namespace
36
DevicesSettings& DevicesSettings::GetDefault()
38
static DevicesSettings instance;
24
42
DevicesSettings::DevicesSettings()
26
_raw_devices_option(1),
27
_devices_option(ONLY_MOUNTED)
29
_settings = g_settings_new("com.canonical.Unity.Devices");
30
g_signal_connect(_settings, "changed",
31
(GCallback)(DevicesSettings::Changed), this);
35
DevicesSettings::~DevicesSettings()
37
g_object_unref(_settings);
41
DevicesSettings::Refresh()
43
_raw_devices_option = g_settings_get_enum(_settings, "devices-option");
45
_devices_option = (DevicesOption)_raw_devices_option;
51
DevicesSettings::Changed(GSettings* settings, char* key, DevicesSettings* self)
57
DevicesSettings::GetDefault()
59
if (G_UNLIKELY(!_devices_settings))
60
_devices_settings = new DevicesSettings();
62
return _devices_settings;
65
DevicesSettings::DevicesOption
66
DevicesSettings::GetDevicesOption()
68
return _devices_option;
43
: settings_(g_settings_new(SETTINGS_NAME))
44
, ignore_signals_(false)
45
, devices_option_(ONLY_MOUNTED)
48
g_signal_connect(settings_, "changed", G_CALLBACK(on_settings_updated), this);
53
void DevicesSettings::Refresh()
55
gchar** favs = g_settings_get_strv(settings_, "favorites");
59
for (int i = 0; favs[i] != NULL; i++)
60
favorites_.push_back(favs[i]);
65
void DevicesSettings::AddFavorite(std::string const& uuid)
70
favorites_.push_back(uuid);
72
SaveFavorites(favorites_);
76
void DevicesSettings::RemoveFavorite(std::string const& uuid)
81
DeviceList::iterator pos = std::find(favorites_.begin(), favorites_.end(), uuid);
82
if (pos == favorites_.end())
85
favorites_.erase(pos);
86
SaveFavorites(favorites_);
90
void DevicesSettings::SaveFavorites(DeviceList const& favorites)
92
const int size = favorites.size();
93
const char* favs[size + 1];
97
for (DeviceList::const_iterator i = favorites.begin(), end = favorites.end();
98
i != end; ++i, ++index)
100
favs[index] = i->c_str();
103
ignore_signals_ = true;
104
if (!g_settings_set_strv(settings_, "favorites", favs))
105
g_warning("Saving favorites failed.");
106
ignore_signals_ = false;
110
void DevicesSettings::Changed(std::string const& key)
120
void DevicesSettings::SetDevicesOption(DevicesOption devices_option)
122
if (devices_option == devices_option_)
125
devices_option_ = devices_option;
132
void on_settings_updated(GSettings* settings,
134
DevicesSettings* self)
136
if (settings and key) {
141
} // anonymous namespace