vsingleinstanceguard.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // A running instance requests to exit.
  24. void exit();
  25. private:
  26. // The count of the entries in the buffer to hold the path of the files to open.
  27. enum { FilesBufCount = 1024 };
  28. struct SharedStruct {
  29. // A magic number to identify if this struct is initialized
  30. int m_magic;
  31. // Next empty entry in m_filesBuf.
  32. int m_filesBufIdx;
  33. // File paths to be opened.
  34. // Encoded in this way with 2 bytes for each size part.
  35. // [size of file1][file1][size of file2][file 2]
  36. // Unicode representation of QString.
  37. ushort m_filesBuf[FilesBufCount];
  38. // Whether other instances ask to show the legal instance.
  39. bool m_askedToShow;
  40. };
  41. // Append @p_file to the shared struct files buffer.
  42. // Returns true if succeeds or false if there is no enough space.
  43. bool appendFileToBuffer(SharedStruct *p_str, const QString &p_file);
  44. bool m_online;
  45. QSharedMemory m_sharedMemory;
  46. static const QString c_memKey;
  47. static const int c_magic;
  48. };
  49. #endif // VSINGLEINSTANCEGUARD_H