main.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*****************************************************************************
  2. * main.cpp - QStarDict, a StarDict clone written with using Qt *
  3. * Copyright (C) 2007 Alexander Rodin *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "application.h"
  20. #ifdef Q_OS_UNIX
  21. #include <QDateTime>
  22. #include <QDir>
  23. #include <QFileInfo>
  24. #include <QSettings>
  25. #include <unistd.h>
  26. #elif defined(Q_OS_WIN) // Q_OS_UNIX
  27. #include <windows.h>
  28. #include <QMessageBox>
  29. #endif // Q_OS_WIN
  30. #ifdef QSTARDICT_WITH_TRANSLATIONS
  31. #include <QLocale>
  32. #include <QTranslator>
  33. #endif // QSTARDICT_WITH_TRANSLATIONS
  34. int main(int argc, char *argv[])
  35. {
  36. QStarDict::Application app(argc, argv);
  37. #ifdef Q_OS_UNIX
  38. QSettings lockFile(QDir::homePath() + "/.qstardict/qstardict.pid", QSettings::IniFormat);
  39. QString lastPid = lockFile.value("LastStart/pid").toString();
  40. if (lastPid.length() && QDir("/proc/" + lastPid).exists() &&
  41. lockFile.value("LastStart/time").toDateTime() == QFileInfo("/proc/" + lastPid).created())
  42. {
  43. qDebug("qstardict: already running");
  44. return 0;
  45. }
  46. lockFile.setValue("LastStart/pid", getpid());
  47. lockFile.setValue("LastStart/time", QFileInfo("/proc/" + QString::number(getpid())).created());
  48. lockFile.sync();
  49. #elif defined(Q_OS_WIN) // Q_OS_UNIX
  50. HANDLE hMutex = CreateMutex(NULL, true, (LPCTSTR)"qstardict");
  51. if (GetLastError() == ERROR_ALREADY_EXISTS)
  52. {
  53. QMessageBox::information(0, "Warning", "QStarDict is already running");
  54. // Strange encoding issue...
  55. // MessageBox(0, (LPCWSTR)"Warning", (LPCWSTR)"QStarDict is already running", MB_ICONWARNING);
  56. return 0;
  57. }
  58. #endif // Q_OS_WIN
  59. return app.exec();
  60. }
  61. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc