~rpadovani/reminders-app/1385429

« back to all changes in this revision

Viewing changes to src/plugin/Evernote/tags.cpp

  • Committer: Tarmac
  • Author(s): Michael Zanetti
  • Date: 2014-10-11 16:31:17 UTC
  • mfrom: (266.2.6 reminders-app-tags)
  • Revision ID: tarmac-20141011163117-k34jgx6jk5zizxpw
Add support for tags. Fixes: https://bugs.launchpad.net/bugs/1379747.

Approved by Riccardo Padovani, Ubuntu Phone Apps Jenkins Bot.

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 reminders
 
5
 *
 
6
 * reminders 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
 * reminders 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
 * Authors: Michael Zanetti <michael.zanetti@canonical.com>
 
19
 */
 
20
 
 
21
#include "tags.h"
 
22
#include "tag.h"
 
23
 
 
24
#include <QDebug>
 
25
 
 
26
Tags::Tags(QObject *parent) :
 
27
    QAbstractListModel(parent)
 
28
{
 
29
    foreach (Tag *tag, NotesStore::instance()->tags()) {
 
30
        m_list.append(tag->guid());
 
31
        connect(tag, &Tag::noteCountChanged, this, &Tags::noteCountChanged);
 
32
    }
 
33
 
 
34
    connect(NotesStore::instance(), &NotesStore::tagsLoadingChanged, this, &Tags::loadingChanged);
 
35
    connect(NotesStore::instance(), &NotesStore::tagsErrorChanged, this, &Tags::errorChanged);
 
36
    connect(NotesStore::instance(), &NotesStore::tagAdded, this, &Tags::tagAdded);
 
37
    connect(NotesStore::instance(), &NotesStore::tagRemoved, this, &Tags::tagRemoved);
 
38
}
 
39
 
 
40
bool Tags::loading() const
 
41
{
 
42
    return NotesStore::instance()->tagsLoading();
 
43
}
 
44
 
 
45
QString Tags::error() const
 
46
{
 
47
    return NotesStore::instance()->tagsError();
 
48
}
 
49
 
 
50
int Tags::count() const
 
51
{
 
52
    return rowCount();
 
53
}
 
54
 
 
55
QVariant Tags::data(const QModelIndex &index, int role) const
 
56
{
 
57
    Tag *tag = NotesStore::instance()->tag(m_list.at(index.row()));
 
58
    switch(role) {
 
59
    case RoleGuid:
 
60
        return tag->guid();
 
61
    case RoleName:
 
62
        return tag->name();
 
63
    case RoleNoteCount:
 
64
        return tag->noteCount();
 
65
    }
 
66
    return QVariant();
 
67
}
 
68
 
 
69
int Tags::rowCount(const QModelIndex &parent) const
 
70
{
 
71
    Q_UNUSED(parent)
 
72
    return m_list.count();
 
73
}
 
74
 
 
75
QHash<int, QByteArray> Tags::roleNames() const
 
76
{
 
77
    QHash<int, QByteArray> roles;
 
78
    roles.insert(RoleGuid, "guid");
 
79
    roles.insert(RoleName, "name");
 
80
    roles.insert(RoleNoteCount, "noteCount");
 
81
    return roles;
 
82
}
 
83
 
 
84
Tag* Tags::tag(int index) const
 
85
{
 
86
    if (index < 0 || index >= m_list.count()) {
 
87
        return nullptr;
 
88
    }
 
89
    return NotesStore::instance()->tag(m_list.at(index));
 
90
}
 
91
 
 
92
void Tags::refresh()
 
93
{
 
94
    NotesStore::instance()->refreshTags();
 
95
}
 
96
 
 
97
void Tags::tagAdded(const QString &guid)
 
98
{
 
99
    Tag *tag = NotesStore::instance()->tag(guid);
 
100
    connect(tag, &Tag::nameChanged, this, &Tags::nameChanged);
 
101
    connect(tag, &Tag::noteCountChanged, this, &Tags::noteCountChanged);
 
102
 
 
103
    beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
 
104
    m_list.append(guid);
 
105
    endInsertRows();
 
106
    emit countChanged();
 
107
}
 
108
 
 
109
void Tags::tagRemoved(const QString &guid)
 
110
{
 
111
    beginRemoveRows(QModelIndex(), m_list.indexOf(guid), m_list.indexOf(guid));
 
112
    m_list.removeAll(guid);
 
113
    endRemoveRows();
 
114
    emit countChanged();
 
115
}
 
116
 
 
117
void Tags::nameChanged()
 
118
{
 
119
    Tag *tag = static_cast<Tag*>(sender());
 
120
    QModelIndex idx = index(m_list.indexOf(tag->guid()));
 
121
    emit dataChanged(idx, idx, QVector<int>() << RoleName);
 
122
}
 
123
 
 
124
void Tags::noteCountChanged()
 
125
{
 
126
    Tag *tag= static_cast<Tag*>(sender());
 
127
    QModelIndex idx = index(m_list.indexOf(tag->guid()));
 
128
    emit dataChanged(idx, idx, QVector<int>() << RoleNoteCount);
 
129
}