Back to index

unity  6.0.0
LauncherEntryRemote.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011-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: Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
00018  *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00019  */
00020 
00021 #include "LauncherEntryRemote.h"
00022 #include <UnityCore/Variant.h>
00023 #include <NuxCore/Logger.h>
00024 
00025 namespace unity
00026 {
00027 
00028 namespace
00029 {
00030 nux::logging::Logger logger("launcher.entry.remote");
00031 }
00032 
00042 LauncherEntryRemote::LauncherEntryRemote(std::string const& dbus_name, GVariant* val)
00043   : _dbus_name(dbus_name)
00044   , _count(0)
00045   , _progress(0.0f)
00046   , _emblem_visible(false)
00047   , _count_visible(false)
00048   , _progress_visible(false)
00049   , _urgent(false)
00050 {
00051   glib::String app_uri;
00052   GVariantIter* prop_iter;
00053 
00054   if (!val || dbus_name.empty())
00055   {
00056     LOG_ERROR(logger) << "Invalid launcher entry remote construction";
00057     return;
00058   }
00059 
00060   /* This will make sure that the values are properly ref_sink'ed and unreff'ed */
00061   glib::Variant values(val);
00062   g_variant_get(values, "(sa{sv})", &app_uri, &prop_iter);
00063 
00064   _app_uri = app_uri.Str();
00065 
00066   Update(prop_iter);
00067   g_variant_iter_free(prop_iter);
00068 }
00069 
00079 std::string const& LauncherEntryRemote::AppUri() const
00080 {
00081   return _app_uri;
00082 }
00083 
00087 std::string const& LauncherEntryRemote::DBusName() const
00088 {
00089   return _dbus_name;
00090 }
00091 
00092 std::string const& LauncherEntryRemote::Emblem() const
00093 {
00094   return _emblem;
00095 }
00096 
00097 long long LauncherEntryRemote::Count() const
00098 {
00099   return _count;
00100 }
00101 
00102 double LauncherEntryRemote::Progress() const
00103 {
00104   return _progress;
00105 }
00106 
00113 glib::Object<DbusmenuClient> const& LauncherEntryRemote::Quicklist() const
00114 {
00115   return _quicklist;
00116 }
00117 
00118 bool LauncherEntryRemote::EmblemVisible() const
00119 {
00120   return _emblem_visible;
00121 }
00122 
00123 bool LauncherEntryRemote::CountVisible() const
00124 {
00125   return _count_visible;
00126 }
00127 
00128 bool LauncherEntryRemote::ProgressVisible() const
00129 {
00130   return _progress_visible;
00131 }
00132 
00133 bool LauncherEntryRemote::Urgent() const
00134 {
00135   return _urgent;
00136 }
00137 
00143 void LauncherEntryRemote::SetDBusName(std::string const& dbus_name)
00144 {
00145   if (_dbus_name == dbus_name)
00146     return;
00147 
00148   std::string old_name(_dbus_name);
00149   _dbus_name = dbus_name;
00150 
00151   /* Remove the quicklist since we can't know if it it'll be valid on the
00152    * new name, and we certainly don't want the quicklist to operate on a
00153    * different name than the rest of the launcher API */
00154   SetQuicklist(nullptr);
00155 
00156   dbus_name_changed.emit(this, old_name);
00157 }
00158 
00159 void LauncherEntryRemote::SetEmblem(std::string const& emblem)
00160 {
00161   if (_emblem == emblem)
00162     return;
00163 
00164   _emblem = emblem;
00165   emblem_changed.emit(this);
00166 }
00167 
00168 void LauncherEntryRemote::SetCount(long long count)
00169 {
00170   if (_count == count)
00171     return;
00172 
00173   _count = count;
00174   count_changed.emit(this);
00175 }
00176 
00177 void LauncherEntryRemote::SetProgress(double progress)
00178 {
00179   if (_progress == progress)
00180     return;
00181 
00182   _progress = progress;
00183   progress_changed.emit(this);
00184 }
00185 
00192 void LauncherEntryRemote::SetQuicklistPath(std::string const& dbus_path)
00193 {
00194   /* Check if existing quicklist have exact same path
00195    * and ignore the change in that case */
00196   if (_quicklist)
00197   {
00198     glib::String ql_path;
00199     g_object_get(_quicklist, DBUSMENU_CLIENT_PROP_DBUS_OBJECT, &ql_path, NULL);
00200 
00201     if (ql_path.Str() == dbus_path)
00202     {
00203       return;
00204     }
00205   }
00206   else if (!_quicklist && dbus_path.empty())
00207     return;
00208 
00209   if (!dbus_path.empty())
00210     _quicklist = dbusmenu_client_new(_dbus_name.c_str(), dbus_path.c_str());
00211   else
00212     _quicklist = nullptr;
00213 
00214   quicklist_changed.emit(this);
00215 }
00216 
00223 void
00224 LauncherEntryRemote::SetQuicklist(DbusmenuClient* quicklist)
00225 {
00226   /* Check if existing quicklist have exact same path as the new one
00227    * and ignore the change in that case. We also assert that the quicklist
00228    * uses the same name as the connection owning this launcher entry */
00229   if (_quicklist)
00230   {
00231     glib::String ql_path, new_ql_path, new_ql_name;
00232 
00233     g_object_get(_quicklist, DBUSMENU_CLIENT_PROP_DBUS_OBJECT, &ql_path, NULL);
00234 
00235     if (quicklist)
00236     {
00237       g_object_get(quicklist, DBUSMENU_CLIENT_PROP_DBUS_OBJECT, &new_ql_path, NULL);
00238       g_object_get(quicklist, DBUSMENU_CLIENT_PROP_DBUS_NAME, &new_ql_name, NULL);
00239     }
00240 
00241     if (quicklist && new_ql_name.Str() != _dbus_name)
00242     {
00243       LOG_ERROR(logger) << "Mismatch between quicklist- and launcher entry owner:"
00244                         << new_ql_name << " and " << _dbus_name << " respectively";
00245       return;
00246     }
00247 
00248     if (new_ql_path.Str() == ql_path.Str())
00249     {
00250       return;
00251     }
00252   }
00253   else if (!_quicklist && !quicklist)
00254     return;
00255 
00256   if (!quicklist)
00257     _quicklist = nullptr;
00258   else
00259     _quicklist = glib::Object<DbusmenuClient>(quicklist, glib::AddRef());
00260 
00261   quicklist_changed.emit(this);
00262 }
00263 
00264 void LauncherEntryRemote::SetEmblemVisible(bool visible)
00265 {
00266   if (_emblem_visible == visible)
00267     return;
00268 
00269   _emblem_visible = visible;
00270   emblem_visible_changed.emit(this);
00271 }
00272 
00273 void LauncherEntryRemote::SetCountVisible(bool visible)
00274 {
00275   if (_count_visible == visible)
00276     return;
00277 
00278   _count_visible = visible;
00279   count_visible_changed.emit(this);
00280 }
00281 
00282 void LauncherEntryRemote::SetProgressVisible(bool visible)
00283 {
00284   if (_progress_visible == visible)
00285     return;
00286 
00287   _progress_visible = visible;
00288   progress_visible_changed.emit(this);
00289 }
00290 
00291 void LauncherEntryRemote::SetUrgent(bool urgent)
00292 {
00293   if (_urgent == urgent)
00294     return;
00295 
00296   _urgent = urgent;
00297   urgent_changed.emit(this);
00298 }
00299 
00303 void LauncherEntryRemote::Update(LauncherEntryRemote::Ptr const& other)
00304 {
00305   /* It's important that we update the DBus name first since it might
00306    * unset the quicklist if it changes */
00307 
00308   if (!other)
00309     return;
00310 
00311   SetDBusName(other->DBusName());
00312 
00313   SetEmblem(other->Emblem());
00314   SetCount(other->Count());
00315   SetProgress(other->Progress());
00316   SetQuicklist(other->Quicklist());
00317   SetUrgent(other->Urgent());
00318 
00319   SetEmblemVisible(other->EmblemVisible());
00320   SetCountVisible(other->CountVisible());
00321   SetProgressVisible(other->ProgressVisible());
00322 }
00323 
00328 void LauncherEntryRemote::Update(GVariantIter* prop_iter)
00329 {
00330   gchar* prop_key;
00331   GVariant* prop_value;
00332 
00333   g_return_if_fail(prop_iter != NULL);
00334 
00335   while (g_variant_iter_loop(prop_iter, "{sv}", &prop_key, &prop_value))
00336   {
00337     if (g_str_equal("emblem", prop_key))
00338       SetEmblem(glib::String(g_variant_dup_string(prop_value, 0)).Str());
00339     else if (g_str_equal("count", prop_key))
00340       SetCount(g_variant_get_int64(prop_value));
00341     else if (g_str_equal("progress", prop_key))
00342       SetProgress(g_variant_get_double(prop_value));
00343     else if (g_str_equal("emblem-visible", prop_key))
00344       SetEmblemVisible(g_variant_get_boolean(prop_value));
00345     else if (g_str_equal("count-visible", prop_key))
00346       SetCountVisible(g_variant_get_boolean(prop_value));
00347     else if (g_str_equal("progress-visible", prop_key))
00348       SetProgressVisible(g_variant_get_boolean(prop_value));
00349     else if (g_str_equal("urgent", prop_key))
00350       SetUrgent(g_variant_get_boolean(prop_value));
00351     else if (g_str_equal("quicklist", prop_key))
00352     {
00353       /* The value is the object path of the dbusmenu */
00354       SetQuicklistPath(glib::String(g_variant_dup_string(prop_value, 0)).Str());
00355     }
00356   }
00357 }
00358 
00359 std::string LauncherEntryRemote::GetName() const
00360 {
00361   // This seems a more appropriate name than LauncherEntryRemote
00362   return "LauncherEntry";
00363 }
00364 
00365 void LauncherEntryRemote::AddProperties(GVariantBuilder* builder)
00366 {
00367   variant::BuilderWrapper(builder)
00368     .add("count", Count())
00369     .add("progress", Progress())
00370     .add("emblem_visible", EmblemVisible())
00371     .add("count_visible", CountVisible())
00372     .add("progress_visible", ProgressVisible())
00373     .add("urgent", Urgent());
00374 }
00375 
00376 } // Namespace