Skip to content

Pass command line filenames to already running instance#1258

Merged
phoerious merged 3 commits intokeepassxreboot:developfrom
frostasm:pass-command-line-filenames-to-already-running-instance
Jan 3, 2018
Merged

Pass command line filenames to already running instance#1258
phoerious merged 3 commits intokeepassxreboot:developfrom
frostasm:pass-command-line-filenames-to-already-running-instance

Conversation

@frostasm
Copy link
Copy Markdown
Contributor

@frostasm frostasm commented Dec 7, 2017

Description

  • Implement drag&drop support in main form to open database files
  • Pass command line filenames to already running instance

Motivation and context

To simplify the opening of files when one copy of the program is running (if was enabled the option "Start only a single instance of KeePassXC")

How has this been tested?

Manually on Linux OS

Screenshots (if appropriate):

Types of changes

  • ✅ New feature (non-breaking change which adds functionality)

Checklist:

  • ✅ I have read the CONTRIBUTING document. [REQUIRED]
  • ✅ My code follows the code style of this project. [REQUIRED]
  • ✅ All new and existing tests passed. [REQUIRED]
  • ✅ I have compiled and verified my code with -DWITH_ASAN=ON. [REQUIRED]
  • ❌ My change requires a change to the documentation and I have updated it accordingly.
  • ❌ I have added tests to cover my changes.

@frostasm frostasm force-pushed the pass-command-line-filenames-to-already-running-instance branch 2 times, most recently from 27daf8f to 4f97872 Compare December 8, 2017 21:02
@frostasm
Copy link
Copy Markdown
Contributor Author

@TheZ3ro maybe you have time to check this PR?

@TheZ3ro
Copy link
Copy Markdown
Contributor

TheZ3ro commented Dec 10, 2017

@frostasm yes of course :)

@frostasm frostasm force-pushed the pass-command-line-filenames-to-already-running-instance branch from 4f97872 to 7506df3 Compare December 16, 2017 17:03
@frostasm
Copy link
Copy Markdown
Contributor Author

@TheZ3ro @droidmonkey please check my changes :)

@frostasm frostasm force-pushed the pass-command-line-filenames-to-already-running-instance branch from 7506df3 to 8b5a810 Compare December 22, 2017 14:17
@frostasm
Copy link
Copy Markdown
Contributor Author

I did not found how to use QTest to test drag&drop. I will be grateful for the advice.

if (config()->get("SingleInstance").toBool()) {
// Attempt to connect to the existing instance
QLocalSocket client;
for (int i = 0; i < 3; i++) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++i

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (!kdbxFiles.isEmpty()) {
event->acceptProposedAction();
}
for(const QString &kdbxFile: kdbxFiles) {
Copy link
Copy Markdown
Member

@phoerious phoerious Dec 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kdbxFiles inside kdbxFilesFromUrls() has gone out of scope here, so reference count should be 1 again, but use asConst() around kdbxFiles nevertheless. Also add a space after for and move the & sign to the type name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phoerious It would be nice if someone of the core developers have added information about how to use range-based for loop with Qt containers to the CONTRIBUTING.md

Good article about Qt containers and C++11 range-based loops

Also add a space after for and move the & sign to the type name.

done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should do that. You need to some understanding of how reference counters and detaching work in Qt. It's not KeePassXC stuff though. It's general Qt knowledge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course these knowledge relates to Qt. But I'm sure that not everyone who sends patches understands how Qt lists and range-based for loops works :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what code reviews are for. :-)

src/main.cpp Outdated
for (int ii = filenames.size()-1; ii >= 0; ii--) {
QString filename = filenames.at(ii);
const QStringList fileNames = config()->get("LastOpenedDatabases").toStringList();
for (const QString &filename: fileNames) {
Copy link
Copy Markdown
Member

@phoerious phoerious Dec 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use asConst() and move & to the type name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asConst() function not needed with constant containers.

Qt A Porting Guide to C++11 Ranged For-Loops - Avoiding Detaching

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ummm... I was pretty sure it was non-const when I wrote that comment, but I must have overlooked it.

src/main.cpp Outdated
for (int ii=0; ii < args.length(); ii++) {
QString filename = args[ii];
const bool pwstdin = parser.isSet(pwstdinOption);
for (const QString &filename: fileNames) {
Copy link
Copy Markdown
Member

@phoerious phoerious Dec 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asConst() and & to type.

client->waitForBytesWritten(WaitTimeoutMSec);
client->disconnectFromServer();
} else {
client->deleteLater();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move this to the top and add a return false after this statement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not understand what I should do :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just move deleteLater() to the top, so you return early. Having a larger if block and then a simple else condition at the end of the function is quite error-prone and I consider it bad style. Better do whatever is in the else block right away and return and then you do what used to be in the if block.

Copy link
Copy Markdown
Contributor Author

@frostasm frostasm Dec 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote function

@frostasm frostasm force-pushed the pass-command-line-filenames-to-already-running-instance branch 3 times, most recently from 450e61c to 411769c Compare December 24, 2017 21:36
@frostasm
Copy link
Copy Markdown
Contributor Author

I am also thinking about adding a unit test to pass file names between running instances. But I don't know how to check the correctness, because the current implementation is based on QApplication class (which is a singleton). Perhaps it makes sense to move the implementation of the checking "SingleInstance" and communication between processes in a separate class?

@frostasm frostasm force-pushed the pass-command-line-filenames-to-already-running-instance branch from 411769c to b3f03ed Compare December 25, 2017 10:45
@phoerious
Copy link
Copy Markdown
Member

phoerious commented Dec 25, 2017

It's hard to do, also because it's quite platform-dependent and raises the question if a whole application can still be seen as a unit. Usually, you want your unit tests to be fine-granular and atomic. If you come up with some good ideas, feel free to implement them. If not, I think this is good enough to be merged.

@TheZ3ro
Copy link
Copy Markdown
Contributor

TheZ3ro commented Jan 3, 2018

@frostasm can you please rebase this? Thanks

@frostasm
Copy link
Copy Markdown
Contributor Author

frostasm commented Jan 3, 2018

of course :)

@frostasm frostasm force-pushed the pass-command-line-filenames-to-already-running-instance branch from b3f03ed to 3720c5e Compare January 3, 2018 14:15
@frostasm
Copy link
Copy Markdown
Contributor Author

frostasm commented Jan 3, 2018

@TheZ3ro done

@phoerious phoerious merged commit ffddcb5 into keepassxreboot:develop Jan 3, 2018
@phoerious
Copy link
Copy Markdown
Member

Thanks!

@frostasm frostasm deleted the pass-command-line-filenames-to-already-running-instance branch January 3, 2018 15:04
@michaellass
Copy link
Copy Markdown
Contributor

Unfortunately this reverted my change from #774 and causes recently opened databases to reopen in reversed order on next program startup. Is that necessary to achieve the purpose of this PR or an was it an oversight?

@droidmonkey
Copy link
Copy Markdown
Member

@michaellass I was wondering why that was a problem again! Definitely an oversight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants