dbhub: Show some information on the currently opened database file · hixixTech/sqlitebrowser@8428c93 · GitHub
Skip to content

Commit 8428c93

Browse files
committed
dbhub: Show some information on the currently opened database file
If the current database file is a clone of a remote database show some information on it in the Remote dock. Even though this information is already nice to have the new tab in the Remote dock is mainly added as a place for future features. Also switch to the new tab automatically when opening a remote database to show the user that this is a tracked database.
1 parent 22c87ff commit 8428c93

7 files changed

Lines changed: 157 additions & 11 deletions

File tree

‎src/MainWindow.cpp‎

Lines changed: 7 additions & 0 deletions

‎src/RemoteDatabase.cpp‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ std::vector<RemoteDatabase::LocalFileInfo> RemoteDatabase::localGetLocalFiles(QS
963963
{
964964
localAssureOpened();
965965

966-
// Query commit id for that file name
966+
// Get all rows for this identity
967967
QString sql = QString("SELECT name, url, commit_id, file, branch FROM local WHERE identity=? ORDER BY url");
968968
sqlite3_stmt* stmt;
969969
if(sqlite3_prepare_v2(m_dbLocal, sql.toUtf8(), -1, &stmt, nullptr) != SQLITE_OK)
@@ -984,9 +984,58 @@ std::vector<RemoteDatabase::LocalFileInfo> RemoteDatabase::localGetLocalFiles(QS
984984
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
985985
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)),
986986
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)),
987-
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)));
987+
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)),
988+
identity.toStdString());
988989
}
989990

990991
sqlite3_finalize(stmt);
991992
return result;
992993
}
994+
995+
RemoteDatabase::LocalFileInfo RemoteDatabase::localGetLocalFileInfo(QString filename)
996+
{
997+
localAssureOpened();
998+
999+
// Find this file in our database
1000+
QString sql = QString("SELECT name, url, commit_id, branch, identity FROM local WHERE file=?");
1001+
sqlite3_stmt* stmt;
1002+
if(sqlite3_prepare_v2(m_dbLocal, sql.toUtf8(), -1, &stmt, nullptr) != SQLITE_OK)
1003+
return {};
1004+
1005+
// Remove the path for querying the file name
1006+
filename = QFileInfo(filename).fileName();
1007+
if(sqlite3_bind_text(stmt, 1, filename.toUtf8(), filename.toUtf8().length(), SQLITE_TRANSIENT))
1008+
{
1009+
sqlite3_finalize(stmt);
1010+
return {};
1011+
}
1012+
1013+
if(sqlite3_step(stmt) != SQLITE_ROW)
1014+
{
1015+
// If there was either an error or no record was found for this file name, stop here.
1016+
sqlite3_finalize(stmt);
1017+
return {};
1018+
}
1019+
1020+
// Retrieve and return all the information we have
1021+
RemoteDatabase::LocalFileInfo result(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
1022+
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
1023+
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)),
1024+
filename.toStdString(),
1025+
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)),
1026+
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)));
1027+
sqlite3_finalize(stmt);
1028+
return result;
1029+
}
1030+
1031+
QString RemoteDatabase::LocalFileInfo::user_name() const
1032+
{
1033+
// Figure out the user name from the URL
1034+
1035+
QString path = QUrl(QString::fromStdString(url)).path();
1036+
1037+
if(path.count('/') < 2 || !path.startsWith('/'))
1038+
return QString();
1039+
else
1040+
return path.mid(1, path.indexOf('/', 1) - 1);
1041+
}

‎src/RemoteDatabase.h‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,38 @@ class RemoteDatabase : public QObject
5050
const QString& commitMessage = QString(), const QString& licence = QString(), bool isPublic = false,
5151
const QString& branch = QString("master"), bool forcePush = false);
5252

53-
// This can be used to get a list of all locally checked out databases for a given identity.
53+
// These function can be used to get information on locally checked out databases
5454
class LocalFileInfo
5555
{
5656
public:
57-
LocalFileInfo(const std::string& _name, const std::string& _url, const std::string& _commit_id, const std::string& _file, const std::string& _branch) :
57+
LocalFileInfo()
58+
{}
59+
60+
LocalFileInfo(const std::string& _name,
61+
const std::string& _url,
62+
const std::string& _commit_id,
63+
const std::string& _file,
64+
const std::string& _branch,
65+
const std::string& _identity) :
5866
name(_name),
5967
url(_url),
6068
commit_id(_commit_id),
6169
file(_file),
62-
branch(_branch)
70+
branch(_branch),
71+
identity(_identity)
6372
{}
6473

74+
QString user_name() const;
75+
6576
std::string name;
6677
std::string url;
6778
std::string commit_id;
6879
std::string file;
6980
std::string branch;
81+
std::string identity;
7082
};
71-
std::vector<LocalFileInfo> localGetLocalFiles(QString identity);
83+
std::vector<LocalFileInfo> localGetLocalFiles(QString identity); // Return a list of all checked out databases for a given identity
84+
LocalFileInfo localGetLocalFileInfo(QString filename); // Return information on a single file
7285

7386
signals:
7487
// As soon as you can safely open a network connection, this signal is emitted. This can be used to delay early network requests

‎src/RemoteDock.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,20 @@ void RemoteDock::openLocalFile(const QModelIndex& idx)
218218
if(!file.isEmpty())
219219
emit openFile(Settings::getValue("remote", "clonedirectory").toString() + "/" + file);
220220
}
221+
222+
void RemoteDock::fileOpened(const QString& filename)
223+
{
224+
// Check if it is a tracked remote database file and retrieve the information we have on it
225+
RemoteDatabase::LocalFileInfo info;
226+
if(filename.startsWith(Settings::getValue("remote", "clonedirectory").toString()))
227+
info = remoteDatabase.localGetLocalFileInfo(filename);
228+
229+
// Copy information to view
230+
ui->labelDatabaseUser->setText(info.user_name());
231+
ui->labelDatabaseFile->setText(QString::fromStdString(info.name));
232+
ui->labelDatabaseBranch->setText(QString::fromStdString(info.branch));
233+
234+
// Switch to "Current Database" tab when we have information on this database
235+
if(!info.file.empty())
236+
ui->tabs->setCurrentIndex(2);
237+
}

‎src/RemoteDock.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class RemoteDock : public QDialog
2323
void reloadSettings();
2424
void enableButtons();
2525

26+
// This function should be called whenever a database file is opened.
27+
// It checks whether the file is a checkout of a tracked remote database
28+
// and updates some of the fields in the remote dock if it is.
29+
// Call it with an empty file name if the database is closed.
30+
void fileOpened(const QString& filename);
31+
2632
public slots:
2733
void reject() override;
2834

‎src/RemoteDock.ui‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,64 @@
125125
</item>
126126
</layout>
127127
</widget>
128+
<widget class="QWidget" name="tabDatabase">
129+
<attribute name="title">
130+
<string>Current Database</string>
131+
</attribute>
132+
<layout class="QFormLayout" name="formLayout">
133+
<item row="0" column="0">
134+
<widget class="QLabel" name="label">
135+
<property name="text">
136+
<string>User</string>
137+
</property>
138+
</widget>
139+
</item>
140+
<item row="0" column="1">
141+
<widget class="QLabel" name="labelDatabaseUser">
142+
<property name="text">
143+
<string/>
144+
</property>
145+
<property name="textInteractionFlags">
146+
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
147+
</property>
148+
</widget>
149+
</item>
150+
<item row="1" column="0">
151+
<widget class="QLabel" name="label_4">
152+
<property name="text">
153+
<string>Database</string>
154+
</property>
155+
</widget>
156+
</item>
157+
<item row="1" column="1">
158+
<widget class="QLabel" name="labelDatabaseFile">
159+
<property name="text">
160+
<string/>
161+
</property>
162+
<property name="textInteractionFlags">
163+
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
164+
</property>
165+
</widget>
166+
</item>
167+
<item row="2" column="0">
168+
<widget class="QLabel" name="label_6">
169+
<property name="text">
170+
<string>Branch</string>
171+
</property>
172+
</widget>
173+
</item>
174+
<item row="2" column="1">
175+
<widget class="QLabel" name="labelDatabaseBranch">
176+
<property name="text">
177+
<string/>
178+
</property>
179+
<property name="textInteractionFlags">
180+
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
181+
</property>
182+
</widget>
183+
</item>
184+
</layout>
185+
</widget>
128186
</widget>
129187
</item>
130188
</layout>

‎src/RemoteLocalFilesModel.cpp‎

Lines changed: 1 addition & 5 deletions

0 commit comments

Comments
 (0)