qwebdatabase.cpp 5.6 KB

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