Back to index

unity  6.0.0
SoftwareCenterLauncherIcon.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: Bilal Akhtar <bilalakhtar@ubuntu.com>
00018  *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00019  */
00020 
00021 #include <NuxCore/Logger.h>
00022 #include <glib/gi18n-lib.h>
00023 #include "SoftwareCenterLauncherIcon.h"
00024 #include "Launcher.h"
00025 #include "LauncherDragWindow.h"
00026 #include "LauncherModel.h"
00027 
00028 namespace unity
00029 {
00030 namespace launcher
00031 {
00032 
00033 NUX_IMPLEMENT_OBJECT_TYPE(SoftwareCenterLauncherIcon);
00034 
00035 SoftwareCenterLauncherIcon::SoftwareCenterLauncherIcon(BamfApplication* app,
00036                                                        std::string const& aptdaemon_trans_id,
00037                                                        std::string const& icon_path)
00038 : BamfLauncherIcon(app),
00039   aptdaemon_trans_("org.debian.apt",
00040                    aptdaemon_trans_id,
00041                    "org.debian.apt.transaction",
00042                    G_BUS_TYPE_SYSTEM,
00043                    G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
00044 , finished_(true)
00045 , needs_urgent_(false)
00046 {
00047 
00048   aptdaemon_trans_.Connect("PropertyChanged", sigc::mem_fun(this, &SoftwareCenterLauncherIcon::OnPropertyChanged));
00049   aptdaemon_trans_.Connect("Finished", sigc::mem_fun(this, &SoftwareCenterLauncherIcon::OnFinished));
00050 
00051   SetIconType(TYPE_APPLICATION);
00052   icon_name = icon_path;
00053   tooltip_text = _("Waiting to install");
00054 }
00055 
00056 void SoftwareCenterLauncherIcon::Animate(nux::ObjectPtr<Launcher> launcher,
00057                                         int icon_x,
00058                                         int icon_y,
00059                                         int icon_size)
00060 {
00061   int target_x = 0;
00062   int target_y = 0;
00063 
00064   launcher_ = launcher;
00065 
00066   icon_texture_ = nux::GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(
00067     launcher->GetIconSize(),
00068     launcher->GetIconSize(),
00069     1,
00070     nux::BITFMT_R8G8B8A8);
00071 
00072   drag_window_ = new LauncherDragWindow(icon_texture_);
00073 
00074   launcher->RenderIconToTexture(nux::GetWindowThread()->GetGraphicsEngine(),
00075                                 AbstractLauncherIcon::Ptr(this),
00076                                 icon_texture_);
00077 
00078   drag_window_->SetBaseXY(icon_x, icon_y);
00079   drag_window_->ShowWindow(true);
00080 
00081   // Find out the center of last BamfLauncherIcon with non-zero co-ordinates
00082   auto bamf_icons = launcher->GetModel()->GetSublist<BamfLauncherIcon>();
00083   //TODO: don't iterate through them and pick the last one, just use back() to get the last one.
00084   for (auto current_bamf_icon : bamf_icons)
00085   {
00086     auto icon_center = current_bamf_icon->GetCenter(launcher->monitor);
00087 
00088     if (icon_center.x != 0 && icon_center.y != 0)
00089     {
00090        target_x = icon_center.x;
00091        target_y = icon_center.y;
00092     }
00093   }
00094 
00095   target_y = target_y + (launcher->GetIconSize() / 2);
00096   drag_window_->SetAnimationTarget(target_x, target_y);
00097 
00098   drag_window_->on_anim_completed = drag_window_->anim_completed.connect(sigc::mem_fun(this, &SoftwareCenterLauncherIcon::OnDragAnimationFinished));
00099   drag_window_->StartAnimation();
00100 }
00101 
00102 void SoftwareCenterLauncherIcon::OnDragAnimationFinished()
00103 {
00104   drag_window_->ShowWindow(false);
00105   launcher_->icon_animation_complete.emit(AbstractLauncherIcon::Ptr(this));
00106   drag_window_ = nullptr;
00107 }
00108 
00109 void SoftwareCenterLauncherIcon::ActivateLauncherIcon(ActionArg arg)
00110 {
00111   if (finished_)
00112   {
00113       if (needs_urgent_)
00114       {
00115           SetQuirk(QUIRK_URGENT, false);
00116           needs_urgent_ = false;
00117       }
00118       BamfLauncherIcon::ActivateLauncherIcon(arg);
00119   }
00120   else
00121       SetQuirk(QUIRK_STARTING, false);
00122 }
00123 
00124 void SoftwareCenterLauncherIcon::OnFinished(GVariant *params)
00125 {
00126    glib::String exit_state;
00127    g_variant_get_child(params, 0, "s", &exit_state);
00128 
00129    if (exit_state.Str() == "exit-success")
00130    {
00131       tooltip_text = BamfName();
00132       SetQuirk(QUIRK_PROGRESS, false);
00133       SetQuirk(QUIRK_URGENT, true);
00134       SetProgress(0.0f);
00135       finished_ = true;
00136       needs_urgent_ = true;
00137    } else {
00138       // failure condition, remove icon again
00139       UnStick();
00140    }
00141 };
00142 
00143 void SoftwareCenterLauncherIcon::OnPropertyChanged(GVariant* params)
00144 {
00145   gint32 progress;
00146   glib::String property_name;
00147 
00148   g_variant_get_child(params, 0, "s", &property_name);
00149 
00150   if (property_name.Str() == "Progress")
00151   {
00152     GVariant* property_value = nullptr;
00153     g_variant_get_child(params, 1, "v", &property_value);
00154     g_variant_get(property_value, "i", &progress);
00155 
00156     if (progress < 100)
00157     {
00158       SetQuirk(QUIRK_PROGRESS, true);
00159       finished_ = false;
00160     }
00161 
00162     SetProgress(progress/100.0f);
00163     g_variant_unref(property_value);
00164   }
00165 }
00166 
00167 std::string SoftwareCenterLauncherIcon::GetName() const
00168 {
00169     return "SoftwareCenterLauncherIcon";
00170 }
00171 
00172 }
00173 }