Back to index

unity  6.0.0
UnitySettings.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003 * Copyright (C) 2010, 2011 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: Neil Jagdish Patel <neil.patel@canonical.com>
00018 */
00019 
00020 #include <gdk/gdk.h>
00021 #include <gio/gio.h>
00022 
00023 #include <NuxCore/Logger.h>
00024 
00025 #include "UnitySettings.h"
00026 #include "UScreen.h"
00027 
00028 namespace unity
00029 {
00030 namespace
00031 {
00032 nux::logging::Logger logger("unity");
00033 
00034 Settings* settings_instance = nullptr;
00035 const char* const FORM_FACTOR = "form-factor";
00036 }
00037 
00038 class Settings::Impl
00039 {
00040 public:
00041   Impl(Settings* owner);
00042   ~Impl();
00043 
00044   FormFactor GetFormFactor() const;
00045   void SetFormFactor(FormFactor factor);
00046 
00047 private:
00048   void Refresh();
00049   static void Changed(GSettings* settings, gchar* key, Impl* self);
00050 
00051 private:
00052   Settings* owner_;
00053   GSettings* settings_;
00054   FormFactor form_factor_;
00055 };
00056 
00057 
00058 Settings::Impl::Impl(Settings* owner)
00059   : owner_(owner)
00060   , settings_(nullptr)
00061   , form_factor_(FormFactor::DESKTOP)
00062 {
00063   settings_ = g_settings_new("com.canonical.Unity");
00064   g_signal_connect(settings_, "changed",
00065                    (GCallback)(Impl::Changed), this);
00066   Refresh();
00067 }
00068 
00069 Settings::Impl::~Impl()
00070 {
00071   g_object_unref(settings_);
00072 }
00073 
00074 void Settings::Impl::Refresh()
00075 {
00076   int raw_from_factor = g_settings_get_enum(settings_, FORM_FACTOR);
00077 
00078   if (raw_from_factor == 0) //Automatic
00079   {
00080     UScreen *uscreen = UScreen::GetDefault();
00081     int primary_monitor = uscreen->GetMonitorWithMouse();
00082     auto geo = uscreen->GetMonitorGeometry(primary_monitor);
00083 
00084     form_factor_ = geo.height > 799 ? FormFactor::DESKTOP : FormFactor::NETBOOK;
00085   }
00086   else
00087   {
00088     form_factor_ = static_cast<FormFactor>(raw_from_factor);
00089   }
00090 
00091   owner_->changed.emit();
00092 }
00093 
00094 void Settings::Impl::Changed(GSettings* settings, char* key, Impl* self)
00095 {
00096   self->Refresh();
00097 }
00098 
00099 FormFactor Settings::Impl::GetFormFactor() const
00100 {
00101   return form_factor_;
00102 }
00103 
00104 void Settings::Impl::SetFormFactor(FormFactor factor)
00105 {
00106   form_factor_ = factor;
00107   g_settings_set_enum(settings_, FORM_FACTOR, static_cast<int>(factor));
00108   owner_->changed.emit();
00109 }
00110 
00111 Settings::Settings()
00112   : is_standalone(false)
00113   , pimpl(new Impl(this))
00114 {
00115   if (settings_instance)
00116   {
00117     LOG_ERROR(logger) << "More than one unity::Settings created.";
00118   }
00119   else
00120   {
00121     settings_instance = this;
00122   }
00123 }
00124 
00125 Settings::~Settings()
00126 {
00127   delete pimpl;
00128   if (settings_instance == this)
00129     settings_instance = nullptr;
00130 }
00131 
00132 Settings& Settings::Instance()
00133 {
00134   if (!settings_instance)
00135   {
00136     LOG_ERROR(logger) << "No unity::Settings created yet.";
00137   }
00138 
00139   return *settings_instance;
00140 }
00141 
00142 FormFactor Settings::GetFormFactor() const
00143 {
00144   return pimpl->GetFormFactor();
00145 }
00146 
00147 void Settings::SetFormFactor(FormFactor factor)
00148 {
00149   pimpl->SetFormFactor(factor);
00150 }
00151 
00152 
00153 } // namespace unity