~3v1n0/unity/scale-window-cast-protection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2012 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Bilal Akhtar <bilalakhtar@ubuntu.com>
 *              Marco Trevisan (Treviño) <3v1n0@ubuntu.com>
 *              Michael Vogt <mvo@ubuntu.com>
 */

#include "config.h"

#include <functional>

#include <NuxCore/Logger.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include "SoftwareCenterLauncherIcon.h"
#include "Launcher.h"
#include "LauncherDragWindow.h"
#include "LauncherModel.h"
#include "DesktopUtilities.h"

namespace unity
{
namespace launcher
{

namespace
{
const int INSTALL_TIP_DURATION = 1500;
}

NUX_IMPLEMENT_OBJECT_TYPE(SoftwareCenterLauncherIcon);
SoftwareCenterLauncherIcon::SoftwareCenterLauncherIcon(ApplicationPtr const& app,
                                                       std::string const& aptdaemon_trans_id)
  : WindowedLauncherIcon(IconType::APPLICATION)
  , ApplicationLauncherIcon(app)
  , aptdaemon_trans_(std::make_shared<glib::DBusProxy>("org.debian.apt",
                                                       aptdaemon_trans_id,
                                                       "org.debian.apt.transaction",
                                                       G_BUS_TYPE_SYSTEM,
                                                       G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START))
  , finished_(false)
  , needs_urgent_(false)
  , aptdaemon_trans_id_(aptdaemon_trans_id)
{
  Stick(false);
  SetQuirk(Quirk::VISIBLE, false);
  SkipQuirkAnimation(Quirk::VISIBLE);

  aptdaemon_trans_->Connect("PropertyChanged", sigc::mem_fun(this, &SoftwareCenterLauncherIcon::OnPropertyChanged));
  aptdaemon_trans_->Connect("Finished", sigc::mem_fun(this, &SoftwareCenterLauncherIcon::OnFinished));
  aptdaemon_trans_->GetProperty("Progress", [this] (GVariant *value) {
    int32_t progress = glib::Variant(value).GetInt32();
    SetProgress(progress/100.0f);
    SetQuirk(Quirk::PROGRESS, (progress > 0));
  });

  if (app->icon_pixbuf())
    icon_pixbuf = app->icon_pixbuf();

  if (!aptdaemon_trans_id_.empty()) // Application is being installed, or hasn't been installed yet
    tooltip_text = _("Waiting to install");
}

void SoftwareCenterLauncherIcon::ActivateLauncherIcon(ActionArg arg)
{
  if (finished_)
  {
    if (needs_urgent_)
    {
      SetQuirk(Quirk::URGENT, false);
      needs_urgent_ = false;
    }

    ApplicationLauncherIcon::ActivateLauncherIcon(arg);
  }
  else
  {
    SetQuirk(Quirk::STARTING, false);
  }
}

std::string SoftwareCenterLauncherIcon::GetActualDesktopFileAfterInstall()
{
  return DesktopUtilities::GetDesktopPathById(DesktopFile());
}

void SoftwareCenterLauncherIcon::OnFinished(GVariant *params)
{
  if (glib::Variant(params).GetString() == "exit-success")
  {
    SetQuirk(Quirk::PROGRESS, false);
    SetQuirk(Quirk::URGENT, true);
    SetProgress(0.0f);
    finished_ = true;
    needs_urgent_ = true;

    // find and update to the real desktop file
    std::string const& new_desktop_path = GetActualDesktopFileAfterInstall();

    // exchange the temp Application with the real one
    auto& app_manager = ApplicationManager::Default();
    auto const& new_app = app_manager.GetApplicationForDesktopFile(new_desktop_path);
    SetApplication(new_app);

    if (new_app)
    {
      Stick();

      _source_manager.AddIdle([this] {
        ShowTooltip();
        _source_manager.AddTimeout(INSTALL_TIP_DURATION, [this] {
          HideTooltip();
          return false;
        });
        return false;
      });
    }
  }
  else
  {
    // failure condition, remove icon again
    Remove();
  }

  aptdaemon_trans_.reset();
};

void SoftwareCenterLauncherIcon::OnPropertyChanged(GVariant* params)
{
  glib::Variant property_name(g_variant_get_child_value(params, 0), glib::StealRef());

  if (property_name.GetString() == "Progress")
  {
    int32_t progress = glib::Variant(g_variant_get_child_value(params, 1), glib::StealRef()).GetInt32();

    if (progress < 100)
    {
      SetQuirk(Quirk::PROGRESS, true);
      tooltip_text = _("Installing…");
    }

    SetProgress(progress/100.0f);
  }
}

std::string SoftwareCenterLauncherIcon::GetName() const
{
  return "SoftwareCenterLauncherIcon";
}

}
}