Allow opening databases in read only mode

See issue #325.
This commit is contained in:
Martin Kleusberg
2017-01-15 20:22:01 +01:00
parent d3dde35f90
commit 50ef6cd7c3
5 changed files with 57 additions and 11 deletions
+11 -5
View File
@@ -263,7 +263,7 @@ void MainWindow::init()
ui->dockSchema->setWindowTitle(ui->dockSchema->windowTitle().remove('&'));
}
bool MainWindow::fileOpen(const QString& fileName, bool dontAddToRecentFiles)
bool MainWindow::fileOpen(const QString& fileName, bool dontAddToRecentFiles, bool readOnly)
{
bool retval = false;
@@ -286,12 +286,12 @@ bool MainWindow::fileOpen(const QString& fileName, bool dontAddToRecentFiles)
return false;
// Try opening it as a project file first
if(loadProject(wFile))
if(loadProject(wFile, readOnly))
{
retval = true;
} else {
// No project file; so it should be a database file
if(db.open(wFile))
if(db.open(wFile, readOnly))
{
statusEncodingLabel->setText(db.getPragma("encoding"));
statusEncryptionLabel->setVisible(db.encrypted());
@@ -2249,7 +2249,7 @@ void MainWindow::updateBrowseDataColumnWidth(int section, int /*old_size*/, int
}
}
bool MainWindow::loadProject(QString filename)
bool MainWindow::loadProject(QString filename, bool readOnly)
{
// Show the open file dialog when no filename was passed as parameter
if(filename.isEmpty())
@@ -2286,7 +2286,7 @@ bool MainWindow::loadProject(QString filename)
QString dbfilename = xml.attributes().value("path").toString();
if(!QFile::exists(dbfilename))
dbfilename = QFileInfo(filename).absolutePath() + QDir::separator() + dbfilename;
fileOpen(dbfilename, true);
fileOpen(dbfilename, true, readOnly);
ui->dbTreeWidget->collapseAll();
} else if(xml.name() == "window") {
// Window settings
@@ -2828,3 +2828,9 @@ void MainWindow::browseDataFetchAllData()
updatePlot(m_currentPlotModel);
}
}
void MainWindow::fileOpenReadOnly()
{
// Redirect to 'standard' fileOpen(), with the read only flag set
fileOpen(QString(), false, true);
}
+3 -2
View File
@@ -196,7 +196,7 @@ protected:
void keyPressEvent(QKeyEvent* event);
public slots:
bool fileOpen(const QString& fileName = QString(), bool dontAddToRecentFiles = false);
bool fileOpen(const QString& fileName = QString(), bool dontAddToRecentFiles = false, bool readOnly = false);
void logSql(const QString &sql, int msgtype);
void dbState(bool dirty);
void refresh();
@@ -265,7 +265,7 @@ private slots:
void on_actionSqlCipherFaq_triggered();
void on_actionWebsite_triggered();
void updateBrowseDataColumnWidth(int section, int /*old_size*/, int new_size);
bool loadProject(QString filename = QString());
bool loadProject(QString filename = QString(), bool readOnly = false);
void saveProject();
void fileAttach();
void updateFilter(int column, const QString& value);
@@ -281,6 +281,7 @@ private slots:
void browseDataSetTableEncoding(bool forAllTables = false);
void browseDataSetDefaultTableEncoding();
void browseDataFetchAllData();
void fileOpenReadOnly();
};
#endif
+39
View File
@@ -873,6 +873,7 @@
</widget>
<addaction name="fileNewAction"/>
<addaction name="fileOpenAction"/>
<addaction name="fileOpenReadOnlyAction"/>
<addaction name="fileAttachAction"/>
<addaction name="fileCloseAction"/>
<addaction name="separator"/>
@@ -1993,6 +1994,27 @@
<string>F5</string>
</property>
</action>
<action name="fileOpenReadOnlyAction">
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/db_open</normaloff>:/icons/db_open</iconset>
</property>
<property name="text">
<string>&amp;Open Database Read Only...</string>
</property>
<property name="toolTip">
<string>Open an existing database file in read only mode</string>
</property>
<property name="statusTip">
<string>Open an existing database file</string>
</property>
<property name="whatsThis">
<string>This option is used to open an existing database file.</string>
</property>
<property name="menuRole">
<enum>QAction::NoRole</enum>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -3039,6 +3061,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>fileOpenReadOnlyAction</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>fileOpenReadOnly()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>518</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
@@ -3101,5 +3139,6 @@
<slot>browseDataSetDefaultTableEncoding()</slot>
<slot>browseDataFetchAllData()</slot>
<slot>exportTableToJson()</slot>
<slot>fileOpenReadOnly()</slot>
</slots>
</ui>
+3 -3
View File
@@ -83,7 +83,7 @@ bool DBBrowserDB::getDirty() const
return !savepointList.empty();
}
bool DBBrowserDB::open(const QString& db)
bool DBBrowserDB::open(const QString& db, bool readOnly)
{
if (isOpen()) close();
@@ -99,7 +99,7 @@ bool DBBrowserDB::open(const QString& db)
}
// Open database file
if(sqlite3_open_v2(db.toUtf8(), &_db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
if(sqlite3_open_v2(db.toUtf8(), &_db, readOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
{
lastErrorMessage = QString::fromUtf8((const char*)sqlite3_errmsg(_db));
return false;
@@ -140,7 +140,7 @@ bool DBBrowserDB::open(const QString& db)
// Check if file is read only
QFileInfo fi(db);
QFileInfo fid(fi.absoluteDir().absolutePath());
isReadOnly = !fi.isWritable() || !fid.isWritable();
isReadOnly = readOnly || !fi.isWritable() || !fid.isWritable();
// Execute default SQL
if(!isReadOnly)
+1 -1
View File
@@ -45,7 +45,7 @@ class DBBrowserDB : public QObject
public:
explicit DBBrowserDB () : _db(0), isEncrypted(false), isReadOnly(false), dontCheckForStructureUpdates(false) {}
virtual ~DBBrowserDB (){}
bool open ( const QString & db);
bool open(const QString& db, bool readOnly = false);
bool attach(const QString& filename, QString attach_as = "");
bool create ( const QString & db);
bool close();