~larryprice/libertine-scope/filter-state

« back to all changes in this revision

Viewing changes to libertine-scope/action.cpp

  • Committer: Tarmac
  • Author(s): Kyle Nitzsche
  • Date: 2016-06-08 20:41:49 UTC
  • mfrom: (37.2.42 filters)
  • Revision ID: tarmac-20160608204149-0uiuyboz25tdrrhw
* Replace the scope settings approach to suppress display of apps with a scope filter based approach. This provides a blacklist file for permanent suppression and filters for user suppression.
* Provide a "Hidden X Apps" department for a place to store the apps hidden in the main scope view, so they can be unhidden later if desired.

Approved by Larry Price, Libertine CI Bot, Christopher Townsend, Stephen M. Webb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 * * Authored by:
 
16
 *      Kyle Nitzsche <kyle.nitzsche@canonical.com>
 
17
 */
 
18
 
 
19
#include "libertine-scope/action.h"
 
20
#include "libertine-scope/config.h"
 
21
#include <unity/scopes/ActivationResponse.h>
 
22
#include <unity/scopes/CannedQuery.h>
 
23
#include <url-dispatcher.h>
 
24
#include <QString>
 
25
#include <QFile>
 
26
#include <QTextStream>
 
27
 
 
28
namespace usc = unity::scopes;
 
29
 
 
30
namespace
 
31
{
 
32
 
 
33
static void
 
34
write_apps_to_hidden(QFile &hidden_f, QStringList const& hidden)
 
35
 
36
  hidden_f.open(QIODevice::WriteOnly | QIODevice::Text);        
 
37
  QTextStream out(&hidden_f);
 
38
  for (auto const& app : hidden)
 
39
  {
 
40
    out << app << "\n";
 
41
  }
 
42
  hidden_f.flush();
 
43
  hidden_f.close();
 
44
}
 
45
 
 
46
} //anonymous namespace
 
47
 
 
48
 
 
49
Action::
 
50
Action(usc::Result const& result,
 
51
       usc::ActionMetadata const& metadata,
 
52
       std::string const& action_id, 
 
53
       std::string const& cache_dir)
 
54
    : usc::ActivationQueryBase(result, metadata),
 
55
      action_id_(action_id),
 
56
      cache_dir_(cache_dir)
 
57
{
 
58
}
 
59
 
 
60
usc::ActivationResponse
 
61
Action::activate()
 
62
{
 
63
  if (action_id_ == "open")
 
64
  {
 
65
    url_dispatch_send(result().uri().c_str() , NULL, NULL);
 
66
    return usc::ActivationResponse(usc::ActivationResponse::Status::NotHandled);
 
67
  }
 
68
  else if (action_id_ == "hide")
 
69
  {
 
70
    QFile file(QString("%1/%2").arg(QString::fromStdString(cache_dir_),"hidden"));
 
71
    file.open(QIODevice::Append | QIODevice::Text);     
 
72
    QTextStream out(&file);
 
73
    out << QString::fromStdString(result()["app_id"].get_string());
 
74
    endl(out);
 
75
    file.close();
 
76
    usc::CannedQuery cq(SCOPE_PKG + "_" + SCOPE_APP);
 
77
    return usc::ActivationResponse(cq);
 
78
  }
 
79
  else if (action_id_ == "show")
 
80
  {
 
81
    QFile hidden_f(QString("%1/%2").arg(QString::fromStdString(cache_dir_), QString::fromStdString("hidden")));
 
82
    if (hidden_f.exists()) {
 
83
      QStringList hidden;
 
84
      if (hidden_f.open(QIODevice::ReadOnly | QIODevice::Text))
 
85
      {
 
86
        QTextStream in(&hidden_f);
 
87
        while (!in.atEnd())
 
88
        {
 
89
          QString line(in.readLine());
 
90
          hidden.append(line.trimmed());
 
91
        }
 
92
        hidden_f.close();
 
93
      }
 
94
      QString app_id = QString::fromStdString(result()["app_id"].get_string());
 
95
      if (hidden.contains(app_id))
 
96
      {
 
97
        hidden.removeAll(app_id);
 
98
        write_apps_to_hidden(hidden_f, hidden);
 
99
      }
 
100
    }
 
101
    usc::CannedQuery cq(SCOPE_PKG + "_" + SCOPE_APP);
 
102
    return usc::ActivationResponse(cq);
 
103
  }
 
104
  return usc::ActivationResponse(usc::ActivationResponse::Status::NotHandled);
 
105
}