~larryprice/libertine-scope/filter-state

« back to all changes in this revision

Viewing changes to libertine-scope/action.cpp

  • Committer: Tarmac
  • Author(s): Larry Price
  • Date: 2016-06-14 14:07:43 UTC
  • mfrom: (45.2.9 minor-cmake-updates)
  • Revision ID: tarmac-20160614140743-z3gy1pl2mqk4k3p8
Refactor Query class for consistent style and extract some functionality to helper classes.

Approved by Christopher Townsend, Libertine CI Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#include "libertine-scope/action.h"
20
20
#include "libertine-scope/config.h"
 
21
#include "libertine-scope/hidden_apps.h"
21
22
#include <unity/scopes/ActivationResponse.h>
22
23
#include <unity/scopes/CannedQuery.h>
23
24
#include <url-dispatcher.h>
27
28
 
28
29
namespace usc = unity::scopes;
29
30
 
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
31
 
49
32
Action::
50
33
Action(usc::Result const& result,
51
34
       usc::ActionMetadata const& metadata,
52
35
       std::string const& action_id, 
53
 
       std::string const& cache_dir)
 
36
       std::shared_ptr<HiddenApps> hidden)
54
37
    : usc::ActivationQueryBase(result, metadata),
55
38
      action_id_(action_id),
56
 
      cache_dir_(cache_dir)
 
39
      hidden_(hidden)
57
40
{
58
41
}
59
42
 
67
50
  }
68
51
  else if (action_id_ == "hide")
69
52
  {
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();
 
53
    hidden_->add(QString::fromStdString(result()["app_id"].get_string()));
 
54
 
76
55
    usc::CannedQuery cq(SCOPE_PKG + "_" + SCOPE_APP);
77
56
    return usc::ActivationResponse(cq);
78
57
  }
79
58
  else if (action_id_ == "show")
80
59
  {
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
 
    }
 
60
    hidden_->remove(QString::fromStdString(result()["app_id"].get_string()));
 
61
 
101
62
    usc::CannedQuery cq(SCOPE_PKG + "_" + SCOPE_APP);
102
63
    return usc::ActivationResponse(cq);
103
64
  }