~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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* Copyright (C) 2011 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: Andrea Azzaronea <azzaronea@gmail.com>
*/

#include <algorithm>
#include <boost/utility.hpp>

#include "FavoriteStorePrivate.h"

namespace unity
{
namespace internal
{
namespace impl
{

std::vector<std::string> GetNewbies(std::list<std::string> const& old, std::list<std::string> const& fresh)
{
  auto sorted_old(old);
  auto sorted_fresh(fresh);

  sorted_old.sort();
  sorted_fresh.sort();

  std::vector<std::string> result;
  std::set_difference(sorted_fresh.begin(), sorted_fresh.end(), sorted_old.begin(), sorted_old.end(),
                      std::inserter(result, result.end()));

  return result;
}

void GetSignalAddedInfo(std::list<std::string> const& favs, std::vector<std::string> const& newbies,
                        std::string const& path, std::string& position, bool& before)
{
  auto it = std::find(favs.begin(), favs.end(), path);
  before = (it == favs.begin());
  position = "";

  if (before and favs.size() > 1)
  {
    while (it != favs.end() && std::find(newbies.begin(), newbies.end(), *it) != newbies.end())
      ++it;

    if (it != favs.end())
      position = *it;
  }
  else if (!before)
  {
    position = *(boost::prior(it));
  }

}

std::vector<std::string> GetRemoved(std::list<std::string> const& old, std::list<std::string> const& fresh)
{
  auto sorted_old(old);
  auto sorted_fresh(fresh);

  sorted_old.sort();
  sorted_fresh.sort();

  std::vector<std::string> result;
  std::set_difference(sorted_old.begin(), sorted_old.end(), sorted_fresh.begin(), sorted_fresh.end(),
                      std::inserter(result, result.end()));

  return result;
}


bool NeedToBeReordered(std::list<std::string> const& old, std::list<std::string> const& fresh)
{
  auto sorted_old(old);
  auto sorted_fresh(fresh);

  sorted_old.sort();
  sorted_fresh.sort();

  std::vector<std::string> ignore_old, ignore_fresh;

  std::set_difference(sorted_old.begin(), sorted_old.end(), sorted_fresh.begin(), sorted_fresh.end(),
                      std::inserter(ignore_old, ignore_old.end()));
  std::set_difference(sorted_fresh.begin(), sorted_fresh.end(), sorted_old.begin(), sorted_old.end(),
                      std::inserter(ignore_fresh, ignore_fresh.end()));

  auto it_old = old.begin();
  auto it_fresh = fresh.begin();

  while (it_old != old.end() && it_fresh != fresh.end())
  {

    while (it_old != old.end() && std::find(ignore_old.begin(), ignore_old.end(), *it_old) != ignore_old.end())
      ++it_old;

    while (it_fresh != fresh.end() && std::find(ignore_fresh.begin(), ignore_fresh.end(), *it_fresh) != ignore_fresh.end())
      ++it_fresh;

    if (it_old == old.end() || it_fresh == fresh.end())
      break;

    if (*it_old != *it_fresh)
    {
      return true;
    }

    ++it_old;
    ++it_fresh;
  }

  return false;
}

bool IsDesktopFilePath(std::string const& path)
{
  static const std::string desktop_ext = ".desktop";
  auto path_len = path.length();
  auto desktop_length = desktop_ext.length();

  if (path_len > desktop_length)
  {
    return path.compare(path_len - desktop_length, desktop_length, desktop_ext) == 0;
  }

  return false;
}


} // namespace impl
} // namespace internal
} // namespace unity