~rpadovani/webbrowser-app/settings-page

« back to all changes in this revision

Viewing changes to click-hooks/hook-utils.h

  • Committer: Alexandre Abreu
  • Date: 2014-11-12 17:00:12 UTC
  • mto: (806.3.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 827.
  • Revision ID: alexandre.abreu@canonical.com-20141112170012-6ov4k5pbqa8ta7sq
draft

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Canonical Ltd.
 
3
 *
 
4
 * This file is part of webbrowser-app.
 
5
 *
 
6
 * webbrowser-app is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * webbrowser-app is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <QDir>
 
20
#include <QHash>
 
21
#include <QString>
 
22
#include <QStringList>
 
23
#include <exception>
 
24
 
 
25
 
 
26
namespace HookUtils {
 
27
 
 
28
/**
 
29
 * Simple optional type wrapper
 
30
 */
 
31
template <typename T>
 
32
class Fallible
 
33
{
 
34
public:
 
35
    explicit Fallible<T>(const T& data, bool is_valid = true)
 
36
        : _data(data), _is_valid(is_valid) {}
 
37
 
 
38
    bool is_valid() const { return _is_valid; }
 
39
    const T& value() const
 
40
    {
 
41
        if (!is_valid())
 
42
        {
 
43
            throw std::exception();
 
44
        }
 
45
        return _data;
 
46
    }
 
47
 
 
48
private:
 
49
    T _data;
 
50
    bool _is_valid;
 
51
};
 
52
 
 
53
/**
 
54
 * @brief The WebappHookParser class
 
55
 */
 
56
class WebappHookParser {
 
57
public:
 
58
    struct Data
 
59
    {
 
60
        Data ()
 
61
            : shouldDeleteCacheOnUninstall(false)
 
62
              , shouldDeleteCookiesOnUninstall(false) {}
 
63
        bool shouldDeleteCacheOnUninstall;
 
64
        bool shouldDeleteCookiesOnUninstall;
 
65
    };
 
66
    typedef Fallible<Data> OptionalData;
 
67
 
 
68
public:
 
69
    OptionalData parseContent(const QString& filename);
 
70
private:
 
71
    OptionalData parseDocument(const QJsonArray &array);
 
72
};
 
73
 
 
74
/**
 
75
 * @brief Simple POD for click hook files & their parent folders.
 
76
 */
 
77
struct WebappClickHookInstallDescription
 
78
{
 
79
    WebappClickHookInstallDescription(
 
80
            const QString& folder,
 
81
            const QHash<QString, QString>& files)
 
82
        : parentFolder(folder), hookFiles(files) {}
 
83
 
 
84
    QString parentFolder;
 
85
    QHash<QString, QString> hookFiles;
 
86
};
 
87
 
 
88
/**
 
89
 * @brief listWebappClickHookFilesIn
 
90
 * @param dir
 
91
 * @return
 
92
 */
 
93
WebappClickHookInstallDescription listWebappClickHookFilesIn(const QDir& dir);
 
94
 
 
95
/**
 
96
 * @brief getProcessedClickHooksFolder
 
97
 * @return
 
98
 */
 
99
QString getProcessedClickHooksFolder();
 
100
 
 
101
/**
 
102
 * @brief getClickHooksInstallFolder
 
103
 * @return
 
104
 */
 
105
QString getClickHooksInstallFolder();
 
106
 
 
107
/**
 
108
 * @brief removeVersionFrom
 
109
 * @param appId
 
110
 * @return
 
111
 */
 
112
QString removeVersionFrom(const QString &appId);
 
113
 
 
114
/**
 
115
 * @brief handleInstalls Detects click package uninstalls and handled what's needed
 
116
 * @param alreadyProcessedClickHooks
 
117
 * @param currentClickHooks
 
118
 */
 
119
void handleInstalls(const WebappClickHookInstallDescription& alreadyProcessedClickHooks
 
120
                   , const WebappClickHookInstallDescription& currentClickHooks);
 
121
 
 
122
/**
 
123
 * @brief handleUninstall Detects click package uninstalls and handled what's needed
 
124
 * @param alreadyProcessedClickHooks
 
125
 * @param currentClickHooks
 
126
 */
 
127
void handleUninstall(const WebappClickHookInstallDescription& alreadyProcessedClickHooks
 
128
                     , const WebappClickHookInstallDescription& currentClickHooks);
 
129
 
 
130
/**
 
131
 * @brief handleUpdates
 
132
 * @param alreadyProcessedClickHooks
 
133
 * @param currentClickHooks
 
134
 */
 
135
void handleUpdates(const WebappClickHookInstallDescription& alreadyProcessedClickHooks
 
136
                   , const WebappClickHookInstallDescription& installedClickHooks);
 
137
 
 
138
} // namespace HookUtils