vsingleinstanceguard.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef VSINGLEINSTANCEGUARD_H
  2. #define VSINGLEINSTANCEGUARD_H
  3. #include <QString>
  4. #include <QSharedMemory>
  5. #include <QStringList>
  6. class VSingleInstanceGuard
  7. {
  8. public:
  9. VSingleInstanceGuard();
  10. // Return ture if this is the only instance of VNote.
  11. bool tryRun();
  12. // There is already another instance running.
  13. // Call this to ask that instance to open external files passed in
  14. // via command line arguments.
  15. void openExternalFiles(const QStringList &p_files);
  16. // Ask another instance to show itself.
  17. void showInstance();
  18. // Fetch files from shared memory to open.
  19. // Will clear the shared memory.
  20. QStringList fetchFilesToOpen();
  21. // Whether this instance is asked to show itself.
  22. bool fetchAskedToShow();
  23. private:
  24. // The count of the entries in the buffer to hold the path of the files to open.
  25. enum { FilesBufCount = 1024 };
  26. struct SharedStruct {
  27. // A magic number to identify if this struct is initialized
  28. int m_magic;
  29. // Next empty entry in m_filesBuf.
  30. int m_filesBufIdx;
  31. // File paths to be opened.
  32. // Encoded in this way with 2 bytes for each size part.
  33. // [size of file1][file1][size of file2][file 2]
  34. // Unicode representation of QString.
  35. ushort m_filesBuf[FilesBufCount];
  36. // Whether other instances ask to show the legal instance.
  37. bool m_askedToShow;
  38. };
  39. // Append @p_file to the shared struct files buffer.
  40. // Returns true if succeeds or false if there is no enough space.
  41. bool appendFileToBuffer(SharedStruct *p_str, const QString &p_file);
  42. QSharedMemory m_sharedMemory;
  43. static const QString c_memKey;
  44. static const int c_magic;
  45. };
  46. #endif // VSINGLEINSTANCEGUARD_H