1/*
2    Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8
9    This library 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 GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18*/
19
20#include "config.h"
21#include "qwebdatabase.h"
22
23#include "qwebdatabase_p.h"
24#include "qwebsecurityorigin.h"
25#include "qwebsecurityorigin_p.h"
26#include "DatabaseDetails.h"
27#include "DatabaseTracker.h"
28
29using namespace WebCore;
30
31/*!
32    \class QWebDatabase
33    \since 4.5
34    \brief The QWebDatabase class provides access to HTML 5 databases created with JavaScript.
35
36    \inmodule QtWebKit
37
38    The upcoming HTML 5 standard includes support for SQL databases that web sites can create and
39    access on a local computer through JavaScript. QWebDatabase is the C++ interface to these
40    databases.
41
42    Databases are grouped together in security origins. To get access to all databases defined by
43    a security origin, use QWebSecurityOrigin::databases(). Each database has an internal name(),
44    as well as a user-friendly name, provided by displayName(). These names are specified when
45    creating the database in the JavaScript code.
46
47    WebKit uses SQLite to create and access the local SQL databases. The location of the database
48    file in the local file system is returned by fileName(). You can access the database directly
49    through the QtSql database module.
50
51    For each database the web site can define an expectedSize(). The current size of the database
52    in bytes is returned by size().
53
54    For more information refer to the \l{http://dev.w3.org/html5/webdatabase/}{HTML5 Web SQL Database Draft Standard}.
55
56    \sa QWebSecurityOrigin
57*/
58
59/*!
60    Constructs a web database from \a other.
61*/
62QWebDatabase::QWebDatabase(const QWebDatabase& other)
63    : d(other.d)
64{
65}
66
67/*!
68    Assigns the \a other web database to this.
69*/
70QWebDatabase& QWebDatabase::operator=(const QWebDatabase& other)
71{
72    d = other.d;
73    return *this;
74}
75
76/*!
77    Returns the name of the database.
78*/
79QString QWebDatabase::name() const
80{
81    return d->name;
82}
83
84/*!
85    Returns the name of the database in a format that is suitable for display to the user.
86*/
87QString QWebDatabase::displayName() const
88{
89#if ENABLE(DATABASE)
90    DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
91    return details.displayName();
92#else
93    return QString();
94#endif
95}
96
97/*!
98    Returns the expected size of the database in bytes as defined by the web author.
99*/
100qint64 QWebDatabase::expectedSize() const
101{
102#if ENABLE(DATABASE)
103    DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
104    return details.expectedUsage();
105#else
106    return 0;
107#endif
108}
109
110/*!
111    Returns the current size of the database in bytes.
112*/
113qint64 QWebDatabase::size() const
114{
115#if ENABLE(DATABASE)
116    DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
117    return details.currentUsage();
118#else
119    return 0;
120#endif
121}
122
123/*!
124    \internal
125*/
126QWebDatabase::QWebDatabase(QWebDatabasePrivate* priv)
127{
128    d = priv;
129}
130
131/*!
132    Returns the file name of the web database.
133
134    The name can be used to access the database through the QtSql database module, for example:
135    \code
136      QWebDatabase webdb = ...
137      QSqlDatabase sqldb = QSqlDatabase::addDatabase("QSQLITE", "myconnection");
138      sqldb.setDatabaseName(webdb.fileName());
139      if (sqldb.open()) {
140          QStringList tables = sqldb.tables();
141          ...
142      }
143    \endcode
144
145    \note Concurrent access to a database from multiple threads or processes
146    is not very efficient because SQLite is used as WebKit's database backend.
147*/
148QString QWebDatabase::fileName() const
149{
150#if ENABLE(DATABASE)
151    return DatabaseTracker::tracker().fullPathForDatabase(d->origin.get(), d->name, false);
152#else
153    return QString();
154#endif
155}
156
157/*!
158    Returns the databases's security origin.
159*/
160QWebSecurityOrigin QWebDatabase::origin() const
161{
162    QWebSecurityOriginPrivate* priv = new QWebSecurityOriginPrivate(d->origin.get());
163    QWebSecurityOrigin origin(priv);
164    return origin;
165}
166
167/*!
168    Removes the database \a db from its security origin. All data stored in the
169    database \a db will be destroyed.
170*/
171void QWebDatabase::removeDatabase(const QWebDatabase& db)
172{
173#if ENABLE(DATABASE)
174    DatabaseTracker::tracker().deleteDatabase(db.d->origin.get(), db.d->name);
175#endif
176}
177
178/*!
179  \since 4.6
180
181  Deletes all web databases in the configured offline storage path.
182
183  \sa QWebSettings::setOfflineStoragePath()
184*/
185void QWebDatabase::removeAllDatabases()
186{
187#if ENABLE(DATABASE)
188    DatabaseTracker::tracker().deleteAllDatabases();
189#endif
190}
191
192/*!
193    Destroys the web database object. The data within this database is \b not destroyed.
194*/
195QWebDatabase::~QWebDatabase()
196{
197}
198
199