~ubuntu-branches/ubuntu/oneiric/arora/oneiric

« back to all changes in this revision

Viewing changes to src/cookiejar/cookiemodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Roderick B. Greening
  • Date: 2009-09-10 15:24:04 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090910152404-668k22ux3mfap6g0
Tags: 0.9.0-0ubuntu1
* New upstream release
* Update patches:
  - kubuntu_02_default_bookmarks.diff
* Remove patches:
  - kubuntu_04_startpage_spacing.diff (fixed upstream)
  - kubuntu_05_manpages.diff (fixed upstream)
  - kubuntu_07_adblock.diff (unstable/unsuitable)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2008-2009 Benjamin C. Meyer <ben@meyerhome.net>
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 as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
 
 * Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
/****************************************************************************
21
 
**
22
 
** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved.
23
 
**
24
 
** This file is part of the demonstration applications of the Qt Toolkit.
25
 
**
26
 
** This file may be used under the terms of the GNU General Public
27
 
** License versions 2.0 or 3.0 as published by the Free Software
28
 
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
29
 
** included in the packaging of this file.  Alternatively you may (at
30
 
** your option) use any later version of the GNU General Public
31
 
** License if such license has been publicly approved by Trolltech ASA
32
 
** (or its successors, if any) and the KDE Free Qt Foundation. In
33
 
** addition, as a special exception, Trolltech gives you certain
34
 
** additional rights. These rights are described in the Trolltech GPL
35
 
** Exception version 1.2, which can be found at
36
 
** http://www.trolltech.com/products/qt/gplexception/ and in the file
37
 
** GPL_EXCEPTION.txt in this package.
38
 
**
39
 
** Please review the following information to ensure GNU General
40
 
** Public Licensing requirements will be met:
41
 
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
42
 
** you are unsure which license is appropriate for your use, please
43
 
** review the following information:
44
 
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
45
 
** or contact the sales department at sales@trolltech.com.
46
 
**
47
 
** In addition, as a special exception, Trolltech, as the sole
48
 
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
49
 
** Integration plug-in the right for the Qt/Eclipse Integration to
50
 
** link to functionality provided by Qt Designer and its related
51
 
** libraries.
52
 
**
53
 
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
54
 
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
55
 
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
56
 
** granted herein.
57
 
**
58
 
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
59
 
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
60
 
**
61
 
****************************************************************************/
62
 
 
63
 
#include "cookiemodel.h"
64
 
 
65
 
#include "cookiejar.h"
66
 
 
67
 
#include <qdatetime.h>
68
 
#include <qfontmetrics.h>
69
 
 
70
 
CookieModel::CookieModel(CookieJar *cookieJar, QObject *parent)
71
 
    : QAbstractTableModel(parent)
72
 
    , m_cookieJar(cookieJar)
73
 
{
74
 
    connect(m_cookieJar, SIGNAL(cookiesChanged()), this, SLOT(cookiesChanged()));
75
 
    m_cookieJar->load();
76
 
}
77
 
 
78
 
QVariant CookieModel::headerData(int section, Qt::Orientation orientation, int role) const
79
 
{
80
 
    if (role == Qt::SizeHintRole) {
81
 
        QFont font;
82
 
        font.setPointSize(10);
83
 
        QFontMetrics fm(font);
84
 
        int height = fm.height() + fm.height() / 3;
85
 
        int width = fm.width(headerData(section, orientation, Qt::DisplayRole).toString());
86
 
        return QSize(width, height);
87
 
    }
88
 
 
89
 
    if (orientation == Qt::Horizontal) {
90
 
        if (role != Qt::DisplayRole)
91
 
            return QVariant();
92
 
 
93
 
        switch (section) {
94
 
        case 0:
95
 
            return tr("Website");
96
 
        case 1:
97
 
            return tr("Name");
98
 
        case 2:
99
 
            return tr("Path");
100
 
        case 3:
101
 
            return tr("Secure");
102
 
        case 4:
103
 
            return tr("Expires");
104
 
        case 5:
105
 
            return tr("Contents");
106
 
        default:
107
 
            return QVariant();
108
 
        }
109
 
    }
110
 
    return QAbstractTableModel::headerData(section, orientation, role);
111
 
}
112
 
 
113
 
QVariant CookieModel::data(const QModelIndex &index, int role) const
114
 
{
115
 
    QList<QNetworkCookie> lst;
116
 
    if (m_cookieJar)
117
 
        lst = m_cookieJar->allCookies();
118
 
    if (index.row() < 0 || index.row() >= lst.size())
119
 
        return QVariant();
120
 
 
121
 
    switch (role) {
122
 
    case Qt::DisplayRole:
123
 
    case Qt::EditRole: {
124
 
        QNetworkCookie cookie = lst.at(index.row());
125
 
        switch (index.column()) {
126
 
        case 0:
127
 
            return cookie.domain();
128
 
        case 1:
129
 
            return cookie.name();
130
 
        case 2:
131
 
            return cookie.path();
132
 
        case 3:
133
 
            return cookie.isSecure() ? tr("true") : tr("false");
134
 
        case 4:
135
 
            return cookie.isSessionCookie() ? tr("Session cookie") : cookie.expirationDate().toString();
136
 
        case 5:
137
 
            return cookie.value();
138
 
        }
139
 
    }
140
 
    case Qt::FontRole: {
141
 
        QFont font;
142
 
        font.setPointSize(10);
143
 
        return font;
144
 
    }
145
 
    }
146
 
 
147
 
    return QVariant();
148
 
}
149
 
 
150
 
int CookieModel::columnCount(const QModelIndex &parent) const
151
 
{
152
 
    return (parent.isValid()) ? 0 : 6;
153
 
}
154
 
 
155
 
int CookieModel::rowCount(const QModelIndex &parent) const
156
 
{
157
 
    return (parent.isValid() || !m_cookieJar) ? 0 : m_cookieJar->allCookies().count();
158
 
}
159
 
 
160
 
bool CookieModel::removeRows(int row, int count, const QModelIndex &parent)
161
 
{
162
 
    if (parent.isValid() || !m_cookieJar)
163
 
        return false;
164
 
    int lastRow = row + count - 1;
165
 
    beginRemoveRows(parent, row, lastRow);
166
 
    QList<QNetworkCookie> lst = m_cookieJar->allCookies();
167
 
    for (int i = lastRow; i >= row; --i) {
168
 
        lst.removeAt(i);
169
 
    }
170
 
    m_cookieJar->setAllCookies(lst);
171
 
    endRemoveRows();
172
 
    return true;
173
 
}
174
 
 
175
 
void CookieModel::cookiesChanged()
176
 
{
177
 
    reset();
178
 
}