mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
single-instance: latter instances will ask the legal instance to show itself
This commit is contained in:
parent
60e65d1781
commit
8264ace8f8
@ -136,6 +136,8 @@ int main(int argc, char *argv[])
|
||||
// Ask another instance to open files passed in.
|
||||
if (!filePaths.isEmpty()) {
|
||||
guard.openExternalFiles(filePaths);
|
||||
} else {
|
||||
guard.showInstance();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -1906,6 +1906,12 @@ void VMainWindow::checkSharedMemory()
|
||||
qDebug() << "shared memory fetch files" << files;
|
||||
openExternalFiles(files);
|
||||
|
||||
// Eliminate the signal.
|
||||
m_guard->fetchAskedToShow();
|
||||
|
||||
showMainWindow();
|
||||
} else if (m_guard->fetchAskedToShow()) {
|
||||
qDebug() << "shared memory asked to show up";
|
||||
showMainWindow();
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ bool VSingleInstanceGuard::tryRun()
|
||||
SharedStruct *str = (SharedStruct *)m_sharedMemory.data();
|
||||
str->m_magic = c_magic;
|
||||
str->m_filesBufIdx = 0;
|
||||
str->m_askedToShow = false;
|
||||
m_sharedMemory.unlock();
|
||||
return true;
|
||||
} else {
|
||||
@ -130,3 +131,37 @@ QStringList VSingleInstanceGuard::fetchFilesToOpen()
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
void VSingleInstanceGuard::showInstance()
|
||||
{
|
||||
if (!m_sharedMemory.isAttached()) {
|
||||
if (!m_sharedMemory.attach()) {
|
||||
qDebug() << "fail to attach to the shared memory segment"
|
||||
<< (m_sharedMemory.error() ? m_sharedMemory.errorString() : "");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_sharedMemory.lock();
|
||||
SharedStruct *str = (SharedStruct *)m_sharedMemory.data();
|
||||
V_ASSERT(str->m_magic == c_magic);
|
||||
str->m_askedToShow = true;
|
||||
m_sharedMemory.unlock();
|
||||
|
||||
qDebug() << "try to request another instance to show up";
|
||||
}
|
||||
|
||||
bool VSingleInstanceGuard::fetchAskedToShow()
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
Q_ASSERT(m_sharedMemory.isAttached());
|
||||
m_sharedMemory.lock();
|
||||
SharedStruct *str = (SharedStruct *)m_sharedMemory.data();
|
||||
Q_ASSERT(str->m_magic == c_magic);
|
||||
ret = str->m_askedToShow;
|
||||
str->m_askedToShow = false;
|
||||
m_sharedMemory.unlock();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -18,10 +18,16 @@ public:
|
||||
// via command line arguments.
|
||||
void openExternalFiles(const QStringList &p_files);
|
||||
|
||||
// Ask another instance to show itself.
|
||||
void showInstance();
|
||||
|
||||
// Fetch files from shared memory to open.
|
||||
// Will clear the shared memory.
|
||||
QStringList fetchFilesToOpen();
|
||||
|
||||
// Whether this instance is asked to show itself.
|
||||
bool fetchAskedToShow();
|
||||
|
||||
private:
|
||||
// The count of the entries in the buffer to hold the path of the files to open.
|
||||
enum { FilesBufCount = 1024 };
|
||||
@ -38,6 +44,9 @@ private:
|
||||
// [size of file1][file1][size of file2][file 2]
|
||||
// Unicode representation of QString.
|
||||
ushort m_filesBuf[FilesBufCount];
|
||||
|
||||
// Whether other instances ask to show the legal instance.
|
||||
bool m_askedToShow;
|
||||
};
|
||||
|
||||
// Append @p_file to the shared struct files buffer.
|
||||
|
Loading…
x
Reference in New Issue
Block a user