Home | History | Annotate | Download | only in Api
      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 
     29 using 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     To get access to all databases defined by a security origin, use QWebSecurityOrigin::databases().
     43     Each database has an internal name(), as well as a user-friendly name, provided by displayName().
     44 
     45     WebKit uses SQLite to create and access the local SQL databases. The location of the database
     46     file in the local file system is returned by fileName(). You can access the database directly
     47     through the QtSql database module.
     48 
     49     For each database the web site can define an expectedSize(). The current size of the database
     50     in bytes is returned by size().
     51 
     52     For more information refer to the \l{http://dev.w3.org/html5/webdatabase/}{HTML 5 Draft Standard}.
     53 
     54     \sa QWebSecurityOrigin
     55 */
     56 
     57 /*!
     58     Constructs a web database from \a other.
     59 */
     60 QWebDatabase::QWebDatabase(const QWebDatabase& other)
     61     : d(other.d)
     62 {
     63 }
     64 
     65 /*!
     66     Assigns the \a other web database to this.
     67 */
     68 QWebDatabase& QWebDatabase::operator=(const QWebDatabase& other)
     69 {
     70     d = other.d;
     71     return *this;
     72 }
     73 
     74 /*!
     75     Returns the name of the database.
     76 */
     77 QString QWebDatabase::name() const
     78 {
     79     return d->name;
     80 }
     81 
     82 /*!
     83     Returns the name of the database as seen by the user.
     84 */
     85 QString QWebDatabase::displayName() const
     86 {
     87 #if ENABLE(DATABASE)
     88     DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
     89     return details.displayName();
     90 #else
     91     return QString();
     92 #endif
     93 }
     94 
     95 /*!
     96     Returns the expected size of the database in bytes as defined by the web author.
     97 */
     98 qint64 QWebDatabase::expectedSize() const
     99 {
    100 #if ENABLE(DATABASE)
    101     DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
    102     return details.expectedUsage();
    103 #else
    104     return 0;
    105 #endif
    106 }
    107 
    108 /*!
    109     Returns the current size of the database in bytes.
    110 */
    111 qint64 QWebDatabase::size() const
    112 {
    113 #if ENABLE(DATABASE)
    114     DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
    115     return details.currentUsage();
    116 #else
    117     return 0;
    118 #endif
    119 }
    120 
    121 /*!
    122     \internal
    123 */
    124 QWebDatabase::QWebDatabase(QWebDatabasePrivate* priv)
    125 {
    126     d = priv;
    127 }
    128 
    129 /*!
    130     Returns the file name of the web database.
    131 
    132     The name can be used to access the database through the QtSql database module, for example:
    133     \code
    134       QWebDatabase webdb = ...
    135       QSqlDatabase sqldb = QSqlDatabase::addDatabase("QSQLITE", "myconnection");
    136       sqldb.setDatabaseName(webdb.fileName());
    137       if (sqldb.open()) {
    138           QStringList tables = sqldb.tables();
    139           ...
    140       }
    141     \endcode
    142 
    143     \note Concurrent access to a database from multiple threads or processes
    144     is not very efficient because SQLite is used as WebKit's database backend.
    145 */
    146 QString QWebDatabase::fileName() const
    147 {
    148 #if ENABLE(DATABASE)
    149     return DatabaseTracker::tracker().fullPathForDatabase(d->origin.get(), d->name, false);
    150 #else
    151     return QString();
    152 #endif
    153 }
    154 
    155 /*!
    156     Returns the databases's security origin.
    157 */
    158 QWebSecurityOrigin QWebDatabase::origin() const
    159 {
    160     QWebSecurityOriginPrivate* priv = new QWebSecurityOriginPrivate(d->origin.get());
    161     QWebSecurityOrigin origin(priv);
    162     return origin;
    163 }
    164 
    165 /*!
    166     Removes the database \a db from its security origin. All data stored in the
    167     database \a db will be destroyed.
    168 */
    169 void QWebDatabase::removeDatabase(const QWebDatabase& db)
    170 {
    171 #if ENABLE(DATABASE)
    172     DatabaseTracker::tracker().deleteDatabase(db.d->origin.get(), db.d->name);
    173 #endif
    174 }
    175 
    176 /*!
    177   \since 4.6
    178 
    179   Deletes all web databases in the configured offline storage path.
    180 
    181   \sa QWebSettings::setOfflineStoragePath()
    182 */
    183 void QWebDatabase::removeAllDatabases()
    184 {
    185 #if ENABLE(DATABASE)
    186     DatabaseTracker::tracker().deleteAllDatabases();
    187 #endif
    188 }
    189 
    190 /*!
    191     Destroys the web database object. The data within this database is \b not destroyed.
    192 */
    193 QWebDatabase::~QWebDatabase()
    194 {
    195 }
    196 
    197