mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-04-06 05:57:37 +03:00
Ask for save location when creating new DB and change default name to 'Passwords.kdbx', resolves #285
This commit is contained in:
parent
012d0ee885
commit
ec17199feb
1 changed files with 71 additions and 65 deletions
|
@ -90,8 +90,16 @@ void DatabaseTabWidget::newDatabase()
|
||||||
db->rootGroup()->setName(tr("Root"));
|
db->rootGroup()->setName(tr("Root"));
|
||||||
dbStruct.dbWidget = new DatabaseWidget(db, this);
|
dbStruct.dbWidget = new DatabaseWidget(db, this);
|
||||||
|
|
||||||
|
CompositeKey emptyKey;
|
||||||
|
db->setKey(emptyKey);
|
||||||
|
|
||||||
insertDatabase(db, dbStruct);
|
insertDatabase(db, dbStruct);
|
||||||
|
|
||||||
|
if (!saveDatabaseAs(db)) {
|
||||||
|
closeDatabase(db);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
dbStruct.dbWidget->switchToMasterKeyChange();
|
dbStruct.dbWidget->switchToMasterKeyChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,80 +356,78 @@ bool DatabaseTabWidget::saveDatabase(Database* db)
|
||||||
|
|
||||||
bool DatabaseTabWidget::saveDatabaseAs(Database* db)
|
bool DatabaseTabWidget::saveDatabaseAs(Database* db)
|
||||||
{
|
{
|
||||||
DatabaseManagerStruct& dbStruct = m_dbList[db];
|
while (true) {
|
||||||
QString oldFileName;
|
DatabaseManagerStruct& dbStruct = m_dbList[db];
|
||||||
if (dbStruct.saveToFilename) {
|
QString oldFileName;
|
||||||
oldFileName = dbStruct.filePath;
|
if (dbStruct.saveToFilename) {
|
||||||
}
|
oldFileName = dbStruct.filePath;
|
||||||
else {
|
} else {
|
||||||
oldFileName = tr("New database").append(".kdbx");
|
oldFileName = tr("Passwords").append(".kdbx");
|
||||||
}
|
|
||||||
QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"),
|
|
||||||
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"),
|
|
||||||
nullptr, 0, "kdbx");
|
|
||||||
if (!fileName.isEmpty()) {
|
|
||||||
QFileInfo fileInfo(fileName);
|
|
||||||
QString lockFilePath;
|
|
||||||
if (fileInfo.exists()) {
|
|
||||||
// returns empty string when file doesn't exist
|
|
||||||
lockFilePath = fileInfo.canonicalPath();
|
|
||||||
}
|
}
|
||||||
else {
|
QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"),
|
||||||
lockFilePath = fileInfo.absolutePath();
|
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"),
|
||||||
}
|
nullptr, 0, "kdbx");
|
||||||
QString lockFileName = QString("%1/.%2.lock").arg(lockFilePath, fileInfo.fileName());
|
if (!fileName.isEmpty()) {
|
||||||
QScopedPointer<QLockFile> lockFile(new QLockFile(lockFileName));
|
QFileInfo fileInfo(fileName);
|
||||||
lockFile->setStaleLockTime(0);
|
QString lockFilePath;
|
||||||
if (!lockFile->tryLock()) {
|
if (fileInfo.exists()) {
|
||||||
// for now silently ignore if we can't create a lock file
|
// returns empty string when file doesn't exist
|
||||||
// due to lack of permissions
|
lockFilePath = fileInfo.canonicalPath();
|
||||||
if (lockFile->error() != QLockFile::PermissionError) {
|
} else {
|
||||||
QMessageBox::StandardButton result = MessageBox::question(this, tr("Save database as"),
|
lockFilePath = fileInfo.absolutePath();
|
||||||
tr("The database you are trying to save as is locked by another instance of KeePassXC.\n"
|
}
|
||||||
"Do you want to save it anyway?"),
|
QString lockFileName = QString("%1/.%2.lock").arg(lockFilePath, fileInfo.fileName());
|
||||||
QMessageBox::Yes | QMessageBox::No);
|
QScopedPointer<QLockFile> lockFile(new QLockFile(lockFileName));
|
||||||
|
lockFile->setStaleLockTime(0);
|
||||||
|
if (!lockFile->tryLock()) {
|
||||||
|
// for now silently ignore if we can't create a lock file
|
||||||
|
// due to lack of permissions
|
||||||
|
if (lockFile->error() != QLockFile::PermissionError) {
|
||||||
|
QMessageBox::StandardButton result = MessageBox::question(this, tr("Save database as"),
|
||||||
|
tr("The database you are trying to save as is locked by another instance of KeePassXC.\n"
|
||||||
|
"Do you want to save it anyway?"),
|
||||||
|
QMessageBox::Yes | QMessageBox::No);
|
||||||
|
|
||||||
if (result == QMessageBox::No) {
|
if (result == QMessageBox::No) {
|
||||||
return false;
|
return false;
|
||||||
}
|
} else {
|
||||||
else {
|
// take over the lock file if possible
|
||||||
// take over the lock file if possible
|
if (lockFile->removeStaleLockFile()) {
|
||||||
if (lockFile->removeStaleLockFile()) {
|
lockFile->tryLock();
|
||||||
lockFile->tryLock();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// setup variables so saveDatabase succeeds
|
// setup variables so saveDatabase succeeds
|
||||||
dbStruct.saveToFilename = true;
|
dbStruct.saveToFilename = true;
|
||||||
dbStruct.canonicalFilePath = fileName;
|
dbStruct.canonicalFilePath = fileName;
|
||||||
|
|
||||||
if (!saveDatabase(db)) {
|
if (!saveDatabase(db)) {
|
||||||
// failed to save, revert back
|
// failed to save, revert back
|
||||||
dbStruct.saveToFilename = false;
|
dbStruct.saveToFilename = false;
|
||||||
dbStruct.canonicalFilePath = oldFileName;
|
dbStruct.canonicalFilePath = oldFileName;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// refresh fileinfo since the file didn't exist before
|
||||||
|
fileInfo.refresh();
|
||||||
|
|
||||||
|
dbStruct.modified = false;
|
||||||
|
dbStruct.saveToFilename = true;
|
||||||
|
dbStruct.readOnly = false;
|
||||||
|
dbStruct.filePath = fileInfo.absoluteFilePath();
|
||||||
|
dbStruct.canonicalFilePath = fileInfo.canonicalFilePath();
|
||||||
|
dbStruct.fileName = fileInfo.fileName();
|
||||||
|
dbStruct.dbWidget->updateFilename(dbStruct.filePath);
|
||||||
|
delete dbStruct.lockFile;
|
||||||
|
dbStruct.lockFile = lockFile.take();
|
||||||
|
updateTabName(db);
|
||||||
|
updateLastDatabases(dbStruct.filePath);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// refresh fileinfo since the file didn't exist before
|
|
||||||
fileInfo.refresh();
|
|
||||||
|
|
||||||
dbStruct.modified = false;
|
|
||||||
dbStruct.saveToFilename = true;
|
|
||||||
dbStruct.readOnly = false;
|
|
||||||
dbStruct.filePath = fileInfo.absoluteFilePath();
|
|
||||||
dbStruct.canonicalFilePath = fileInfo.canonicalFilePath();
|
|
||||||
dbStruct.fileName = fileInfo.fileName();
|
|
||||||
dbStruct.dbWidget->updateFilename(dbStruct.filePath);
|
|
||||||
delete dbStruct.lockFile;
|
|
||||||
dbStruct.lockFile = lockFile.take();
|
|
||||||
updateTabName(db);
|
|
||||||
updateLastDatabases(dbStruct.filePath);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue