void MainWindow::doPreview() { if (fileNameString.isEmpty() || !QFile::exists(fileNameString)) { setStatusError("No file to preview! (None is selected, or maybe it has been deleted)"); return; } QStringList previewCommand; previewCommand << "smplayer"; previewCommand << fileNameString; KProcess::execute(previewCommand); }
int ArchDaemonWindows::doRunDaemon(RunFunc run) { // should only be called from DaemonFunc assert(m_serviceMutex != NULL); assert(run != NULL); // create message queue for this thread MSG dummy; PeekMessage(&dummy, NULL, 0, 0, PM_NOREMOVE); int result = 0; ARCH->lockMutex(m_serviceMutex); m_daemonThreadID = GetCurrentThreadId(); while (m_serviceState != SERVICE_STOPPED) { // wait until we're told to start while (!isRunState(m_serviceState) && m_serviceState != SERVICE_STOP_PENDING) { ARCH->waitCondVar(m_serviceCondVar, m_serviceMutex, -1.0); } // run unless told to stop if (m_serviceState != SERVICE_STOP_PENDING) { ARCH->unlockMutex(m_serviceMutex); try { result = run(); } catch (...) { ARCH->lockMutex(m_serviceMutex); setStatusError(0); m_serviceState = SERVICE_STOPPED; setStatus(m_serviceState); ARCH->broadcastCondVar(m_serviceCondVar); ARCH->unlockMutex(m_serviceMutex); throw; } ARCH->lockMutex(m_serviceMutex); } // notify of new state if (m_serviceState == SERVICE_PAUSE_PENDING) { m_serviceState = SERVICE_PAUSED; } else { m_serviceState = SERVICE_STOPPED; } setStatus(m_serviceState); ARCH->broadcastCondVar(m_serviceCondVar); } ARCH->unlockMutex(m_serviceMutex); return result; }
void ArchDaemonWindows::serviceMain(DWORD argc, LPTSTR* argvIn) { typedef std::vector<LPCTSTR> ArgList; typedef std::vector<std::string> Arguments; const char** argv = const_cast<const char**>(argvIn); // create synchronization objects m_serviceMutex = ARCH->newMutex(); m_serviceCondVar = ARCH->newCondVar(); // register our service handler function m_statusHandle = RegisterServiceCtrlHandler(argv[0], &ArchDaemonWindows::serviceHandlerEntry); if (m_statusHandle == 0) { // cannot start as service m_daemonResult = -1; ARCH->closeCondVar(m_serviceCondVar); ARCH->closeMutex(m_serviceMutex); return; } // tell service control manager that we're starting m_serviceState = SERVICE_START_PENDING; setStatus(m_serviceState, 0, 10000); std::string commandLine; // if no arguments supplied then try getting them from the registry. // the first argument doesn't count because it's the service name. Arguments args; ArgList myArgv; if (argc <= 1) { // read command line HKEY key = openNTServicesKey(); key = ArchMiscWindows::openKey(key, argvIn[0]); key = ArchMiscWindows::openKey(key, _T("Parameters")); if (key != NULL) { commandLine = ArchMiscWindows::readValueString(key, _T("CommandLine")); } // if the command line isn't empty then parse and use it if (!commandLine.empty()) { // parse, honoring double quoted substrings std::string::size_type i = commandLine.find_first_not_of(" \t"); while (i != std::string::npos && i != commandLine.size()) { // find end of string std::string::size_type e; if (commandLine[i] == '\"') { // quoted. find closing quote. ++i; e = commandLine.find("\"", i); // whitespace must follow closing quote if (e == std::string::npos || (e + 1 != commandLine.size() && commandLine[e + 1] != ' ' && commandLine[e + 1] != '\t')) { args.clear(); break; } // extract args.push_back(commandLine.substr(i, e - i)); i = e + 1; } else { // unquoted. find next whitespace. e = commandLine.find_first_of(" \t", i); if (e == std::string::npos) { e = commandLine.size(); } // extract args.push_back(commandLine.substr(i, e - i)); i = e + 1; } // next argument i = commandLine.find_first_not_of(" \t", i); } // service name goes first myArgv.push_back(argv[0]); // get pointers for (size_t j = 0; j < args.size(); ++j) { myArgv.push_back(args[j].c_str()); } // adjust argc/argv argc = (DWORD)myArgv.size(); argv = &myArgv[0]; } } m_commandLine = commandLine; try { // invoke daemon function m_daemonResult = m_daemonFunc(static_cast<int>(argc), argv); } catch (XArchDaemonRunFailed& e) { setStatusError(e.m_result); m_daemonResult = -1; } catch (...) { setStatusError(1); m_daemonResult = -1; } // clean up ARCH->closeCondVar(m_serviceCondVar); ARCH->closeMutex(m_serviceMutex); // we're going to exit now, so set status to stopped m_serviceState = SERVICE_STOPPED; setStatus(m_serviceState, 0, 10000); }