int main(int argc, char *argv[]) { if( argc != 2 ) { std::cout << "xstartonce 0.0.2\n" << "Usage: xstartonce <hotkey-name>\n" << "You need to define hotkeys in ~/.xstartonce in the format \"name=command --params\"."; return 1; } Config config; loadFile(QDir::home().absoluteFilePath(".xstartonce"), config); QString dbPath = QDir::temp().absoluteFilePath("xstartonce-db.%1").arg(userName()); for(Config::const_iterator i = config.begin(); i != config.end(); ++i) { qDebug() << i.key(); if( i.key() == argv[1] ) { qDebug() << "Found key" << i.key(); ProcList procs; if( loadFile(dbPath, procs) ) for(ProcList::const_iterator j = procs.begin(); j != procs.end(); ++j) { if( j.key() == i.key() ) { QString winId = findWindowForPid(qvariant_cast<int>(j.value())); if( !winId.isEmpty() ) { QProcess proc; QStringList args; args << "-i" << "-a" << winId; qDebug() << "Activating window" << winId; proc.start("wmctrl", args); proc.waitForFinished(2000); return 0; } else { qDebug() << "Could not find window for pid" << j.value(); break; } } } int i_pid = startProcess(i.value()); if( i_pid > 0 ) { QString pid = qvariant_cast<QString>(i_pid); procs[i.key()] = pid; writeFile(dbPath, procs); } return 0; } } qDebug() << "No such configuration " << argv[1] << "."; return 3; }
void writeFile(const QString fileName, Config &config) { std::ofstream file(fileName.toLatin1()); for(Config::const_iterator i = config.begin(); i != config.end(); ++i) { QString line = QString("%1=%2\n").arg(i.key(), i.value()); file.write(line.toLatin1(), line.length()); } }
bool KdeIni::close() { QFile file(localFile); if (!file.open(QIODevice::WriteOnly)) return false; Config::const_iterator group = local.constBegin(); Entries::const_iterator entry; QTextStream stream(&file); while (group != local.constEnd()) { stream << '[' << group.key() << ']' << endl; entry = group.value().constBegin(); while (entry != group.value().constEnd()) { stream << entry.key() << '=' << entry.value() << endl; ++entry; } stream << endl; ++group; } stream.flush(); file.close(); return true; }