~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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* Copyright (C) 2010-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: Neil Jagdish Patel <neil.patel@canonical.com>
*              Marco Trevisan <marco.trevisan@canonical.com>
*/

#include <NuxCore/Logger.h>
#include <glib.h>

#include "FavoriteStore.h"
#include "FavoriteStorePrivate.h"

namespace unity
{
DECLARE_LOGGER(logger, "unity.favorite.store");
namespace
{
FavoriteStore* favoritestore_instance = nullptr;
const std::string PREFIX_SEPARATOR = "://";
}

const std::string FavoriteStore::URI_PREFIX_APP = "application://";
const std::string FavoriteStore::URI_PREFIX_FILE = "file://";
const std::string FavoriteStore::URI_PREFIX_DEVICE = "device://";
const std::string FavoriteStore::URI_PREFIX_UNITY = "unity://";

FavoriteStore::FavoriteStore()
{
  if (favoritestore_instance)
  {
    LOG_ERROR(logger) << "More than one FavoriteStore created!";
  }
  else
  {
    favoritestore_instance = this;
  }
}

FavoriteStore::~FavoriteStore()
{
  if (favoritestore_instance == this)
    favoritestore_instance = nullptr;
}


FavoriteStore& FavoriteStore::Instance()
{
  if (! favoritestore_instance)
  {
    LOG_ERROR(logger) << "No FavoriteStore instance created yet!";
  }
  return *favoritestore_instance;
}

bool FavoriteStore::IsValidFavoriteUri(std::string const& uri)
{
  if (uri.empty())
    return false;

  if (uri.find(URI_PREFIX_APP) == 0 || uri.find(URI_PREFIX_FILE) == 0)
  {
    return internal::impl::IsDesktopFilePath(uri);
  }
  else if (uri.find(URI_PREFIX_DEVICE) == 0)
  {
    return uri.length() > URI_PREFIX_DEVICE.length();
  }
  else if (uri.find(URI_PREFIX_UNITY) == 0)
  {
    return uri.length() > URI_PREFIX_UNITY.length();
  }

  return false;
}

std::string FavoriteStore::ParseFavoriteFromUri(std::string const& uri) const
{
  if (uri.empty())
    return "";

  std::string fav = uri;
  auto prefix_pos = fav.find(PREFIX_SEPARATOR);

  if (prefix_pos == std::string::npos)
  {
    // We assume that favorites with no prefix, but with a .desktop suffix are applications
    if (internal::impl::IsDesktopFilePath(uri))
    {
      fav = URI_PREFIX_APP + fav;
      prefix_pos = URI_PREFIX_APP.length();
    }
  }
  else
  {
    prefix_pos += PREFIX_SEPARATOR.length();
  }

  // Matches application://desktop-id.desktop or application:///path/to/file.desktop
  if (fav.find(URI_PREFIX_APP) == 0 || fav.find(URI_PREFIX_FILE) == 0)
  {
    std::string const& fav_value = fav.substr(prefix_pos);

    if (fav_value.empty())
    {
      LOG_WARNING(logger) << "Unable to load Favorite for uri '" << fav << "'";
      return "";
    }

    if (fav_value[0] == '/' || fav.find(URI_PREFIX_FILE) == 0)
    {
      if (g_file_test(fav_value.c_str(), G_FILE_TEST_EXISTS))
      {
        return fav;
      }
      else
      {
        LOG_WARNING(logger) << "Unable to load desktop file: " << fav_value;
      }
    }
    else
    {
      return URI_PREFIX_APP + fav_value;
    }
  }
  else if (IsValidFavoriteUri(fav))
  {
    return fav;
  }

  LOG_WARNING(logger) << "Unable to load Favorite for uri '" << fav << "'";
  return "";
}

}