qtmobility/tools/vsexplorer/vsexplorer.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
   158     QSocketNotifier *sock;
   158     QSocketNotifier *sock;
   159 #endif
   159 #endif
   160 };
   160 };
   161 
   161 
   162 VSExplorer::VSExplorer()
   162 VSExplorer::VSExplorer()
   163 : isSuppress(false), pwd("/"), prov("/")
   163 : isSuppress(false), pwd(QLatin1String("/")), prov(QLatin1String("/"))
   164 {
   164 {
   165 }
   165 }
   166 
   166 
   167 void VSExplorer::interestChanged(const QString &attribute, bool interested)
   167 void VSExplorer::interestChanged(const QString &attribute, bool interested)
   168 {
   168 {
   176 }
   176 }
   177 
   177 
   178 static QString variantToString( const QVariant& var )
   178 static QString variantToString( const QVariant& var )
   179 {
   179 {
   180     if ( var.type() == QVariant::StringList )
   180     if ( var.type() == QVariant::StringList )
   181         return var.toStringList().join(", ");
   181         return var.toStringList().join(QLatin1String(", "));
   182     else
   182     else
   183         return var.toString();
   183         return var.toString();
   184 }
   184 }
   185 
   185 
   186 void VSExplorer::contentsChanged()
   186 void VSExplorer::contentsChanged()
   232 }
   232 }
   233 
   233 
   234 
   234 
   235 void VSExplorer::lsPath(QValueSpaceSubscriber * p, int indent, bool showHidden)
   235 void VSExplorer::lsPath(QValueSpaceSubscriber * p, int indent, bool showHidden)
   236 {
   236 {
   237     QStringList paths = p->subPaths();
       
   238 
       
   239     QVariant var = p->value();
   237     QVariant var = p->value();
   240     bool spaceRequired = false;
   238     bool spaceRequired = false;
   241     if(!var.isNull()) {
   239     if(!var.isNull()) {
   242         fprintf(stdout, "Value: '%s' (%s)\n",
   240         fprintf(stdout, "Value: '%s' (%s)\n",
   243                 variantToString(var).toAscii().constData(), var.typeName());
   241                 variantToString(var).toAscii().constData(), var.typeName());
   244         spaceRequired = true;
   242         spaceRequired = true;
   245     }
   243     }
   246 
   244 
   247     foreach(QString path, paths) {
   245     foreach (const QString &path, p->subPaths()) {
   248         if(!showHidden && path.startsWith("."))
   246         if (!showHidden && path.startsWith(QLatin1Char('.')))
   249             continue;
   247             continue;
   250 
   248 
   251         if(spaceRequired) {
   249         if(spaceRequired) {
   252             fprintf(stdout, "\n");
   250             fprintf(stdout, "\n");
   253             spaceRequired = false;
   251             spaceRequired = false;
   368     fflush(stdout);
   366     fflush(stdout);
   369 }
   367 }
   370 
   368 
   371 void VSExplorer::set(const QString &name, const QString &value)
   369 void VSExplorer::set(const QString &name, const QString &value)
   372 {
   370 {
   373     if('/' == *name.constData())
   371     if (name.startsWith(QLatin1Char('/')))
   374         prov.setValue(name, value);
   372         prov.setValue(name, value);
   375     else if(pwd.path().endsWith("/"))
   373     else if (pwd.path().endsWith(QLatin1Char('/')))
   376         prov.setValue(pwd.path() + name, value);
   374         prov.setValue(pwd.path() + name, value);
   377     else
   375     else
   378         prov.setValue(pwd.path() + "/" + name, value);
   376         prov.setValue(pwd.path() + QLatin1Char('/') + name, value);
   379 }
   377 }
   380 
   378 
   381 void VSExplorer::clear(const QString &name)
   379 void VSExplorer::clear(const QString &name)
   382 {
   380 {
   383     if('/' == *name.constData())
   381     if (name.startsWith(QLatin1Char('/')))
   384         prov.resetValue(name);
   382         prov.resetValue(name);
   385     else if(pwd.path().endsWith("/"))
   383     else if (pwd.path().endsWith(QLatin1Char('/')))
   386         prov.resetValue(pwd.path() + name);
   384         prov.resetValue(pwd.path() + name);
   387     else
   385     else
   388         prov.resetValue(pwd.path() + "/" + name);
   386         prov.resetValue(pwd.path() + QLatin1Char('/') + name);
   389 }
   387 }
   390 
   388 
   391 void VSExplorer::processLine(const QString &line)
   389 void VSExplorer::processLine(const QString &line)
   392 {
   390 {
   393     QStringList cmds = line.trimmed().split(' ');
   391     QStringList cmds = line.trimmed().split(QLatin1Char(' '));
   394 
   392 
   395     if(cmds.isEmpty()) {
   393     if(cmds.isEmpty()) {
   396         return;
   394         return;
   397     }
   395     }
   398 
   396 
   399     const QString & cmd = cmds.at(0);
   397     const QString & cmd = cmds.at(0);
   400     if(cmd == "ls" && 1 == cmds.count()) {
   398     if (cmd.isEmpty()) {
       
   399 
       
   400     } else if (cmd == QLatin1String("ls") && 1 == cmds.count()) {
   401         ls();
   401         ls();
   402     } else if(cmd == "dump") {
   402     } else if (cmd == QLatin1String("dump")) {
   403         dump();
   403         dump();
   404     } else if(cmd == "pwd") {
   404     } else if (cmd == QLatin1String("pwd")) {
   405         pwdCmd();
   405         pwdCmd();
   406     } else if(cmd == "ls" && 2 <= cmds.count()) {
   406     } else if (cmd == QLatin1String("ls") && 2 <= cmds.count()) {
   407         QStringList newCmds = cmds;
   407         QStringList newCmds = cmds;
   408         newCmds.removeFirst();
   408         newCmds.removeFirst();
   409         bool lsAll = false;
   409         bool lsAll = false;
   410         if(newCmds.first() == "-a") {
   410         if (newCmds.first() == QLatin1String("-a")) {
   411             lsAll = true;
   411             lsAll = true;
   412             newCmds.removeFirst();
   412             newCmds.removeFirst();
   413         }
   413         }
   414         QString newPath = newCmds.join(" ");
   414         QString newPath = newCmds.join(QLatin1String(" "));
   415         if(newPath.startsWith("\"") && newPath.endsWith("\"")) {
   415         if (newPath.startsWith(QLatin1Char('"')) && newPath.endsWith(QLatin1Char('"'))) {
   416             newPath = newPath.mid(1);
   416             newPath = newPath.mid(1);
   417             newPath = newPath.left(newPath.length() - 1);
   417             newPath = newPath.left(newPath.length() - 1);
   418         }
   418         }
   419         if(newPath.isEmpty()) {
   419         if(newPath.isEmpty()) {
   420             ls(pwd.path().toAscii(), lsAll);
   420             ls(pwd.path(), lsAll);
   421         } else if(newPath.startsWith("/")) {
   421         } else if (newPath.startsWith(QLatin1Char('/'))) {
   422             ls(newPath.toAscii(), lsAll);
   422             ls(newPath, lsAll);
   423         } else {
   423         } else {
   424             QString oldPath = pwd.path();
   424             QString oldPath = pwd.path();
   425             if(!oldPath.endsWith("/"))
   425             if (!oldPath.endsWith(QLatin1Char('/')))
   426                 oldPath.append("/");
   426                 oldPath.append(QLatin1Char('/'));
   427             oldPath.append(newPath);
   427             oldPath.append(newPath);
   428             oldPath = QDir::cleanPath(oldPath);
   428             oldPath = QDir::cleanPath(oldPath);
   429             ls(oldPath.toAscii(), lsAll);
   429             ls(oldPath, lsAll);
   430         }
   430         }
   431 
   431 
   432     } else if(cmd == "cd" && 2 <= cmds.count()) {
   432     } else if (cmd == QLatin1String("cd") && 2 <= cmds.count()) {
   433         QStringList newCmds = cmds;
   433         QStringList newCmds = cmds;
   434         newCmds.removeFirst();
   434         newCmds.removeFirst();
   435         QString newPath = newCmds.join(" ");
   435         QString newPath = newCmds.join(QLatin1String(" "));
   436         if(newPath.startsWith("\"") && newPath.endsWith("\"")) {
   436         if (newPath.startsWith(QLatin1Char('"')) && newPath.endsWith(QLatin1Char('"'))) {
   437             newPath = newPath.mid(1);
   437             newPath = newPath.mid(1);
   438             newPath = newPath.left(newPath.length() - 1);
   438             newPath = newPath.left(newPath.length() - 1);
   439         }
   439         }
   440         if(newPath.startsWith("/")) {
   440         if (newPath.startsWith(QLatin1Char('/'))) {
   441             pwd.setPath(newPath);
   441             pwd.setPath(newPath);
   442         } else {
   442         } else {
   443             QString oldPath = pwd.path();
   443             QString oldPath = pwd.path();
   444             if(!oldPath.endsWith("/"))
   444             if (!oldPath.endsWith(QLatin1Char('/')))
   445                 oldPath.append("/");
   445                 oldPath.append(QLatin1Char('/'));
   446             oldPath.append(newPath);
   446             oldPath.append(newPath);
   447             oldPath = QDir::cleanPath(oldPath);
   447             oldPath = QDir::cleanPath(oldPath);
   448             pwd.setPath(oldPath);
   448             pwd.setPath(oldPath);
   449         }
   449         }
   450     } else if(cmd == "unwatch" && 2 <= cmds.count()) {
   450     } else if (cmd == QLatin1String("unwatch") && 2 <= cmds.count()) {
   451         QStringList newCmds = cmds;
   451         QStringList newCmds = cmds;
   452         newCmds.removeFirst();
   452         newCmds.removeFirst();
   453         QString newPath = newCmds.join(" ");
   453         QString newPath = newCmds.join(QLatin1String(" "));
   454         QString finalPath;
   454         QString finalPath;
   455         if(newPath.startsWith("\"") && newPath.endsWith("\"")) {
   455         if (newPath.startsWith(QLatin1Char('"')) && newPath.endsWith(QLatin1Char('"'))) {
   456             newPath = newPath.mid(1);
   456             newPath = newPath.mid(1);
   457             newPath = newPath.left(newPath.length() - 1);
   457             newPath = newPath.left(newPath.length() - 1);
   458         }
   458         }
   459         if(newPath.startsWith("/")) {
   459         if (newPath.startsWith(QLatin1Char('/'))) {
   460             finalPath = QValueSpaceSubscriber(newPath).path();
   460             finalPath = QValueSpaceSubscriber(newPath).path();
   461         } else {
   461         } else {
   462             QString oldPath = pwd.path();
   462             QString oldPath = pwd.path();
   463             if(!oldPath.endsWith("/"))
   463             if (!oldPath.endsWith(QLatin1Char('/')))
   464                 oldPath.append("/");
   464                 oldPath.append(QLatin1Char('/'));
   465             oldPath.append(newPath);
   465             oldPath.append(newPath);
   466             oldPath = QDir::cleanPath(oldPath);
   466             oldPath = QDir::cleanPath(oldPath);
   467             finalPath = QValueSpaceSubscriber(oldPath).path();
   467             finalPath = QValueSpaceSubscriber(oldPath).path();
   468         }
   468         }
   469         unwatch(finalPath.toUtf8());
   469         unwatch(finalPath);
   470     } else if(cmd == "watch" && 2 <= cmds.count()) {
   470     } else if (cmd == QLatin1String("watch") && 2 <= cmds.count()) {
   471         QStringList newCmds = cmds;
   471         QStringList newCmds = cmds;
   472         newCmds.removeFirst();
   472         newCmds.removeFirst();
   473         QString newPath = newCmds.join(" ");
   473         QString newPath = newCmds.join(QLatin1String(" "));
   474         QString finalPath;
   474         QString finalPath;
   475         if(newPath.startsWith("\"") && newPath.endsWith("\"")) {
   475         if (newPath.startsWith(QLatin1Char('"')) && newPath.endsWith(QLatin1Char('"'))) {
   476             newPath = newPath.mid(1);
   476             newPath = newPath.mid(1);
   477             newPath = newPath.left(newPath.length() - 1);
   477             newPath = newPath.left(newPath.length() - 1);
   478         }
   478         }
   479         if(newPath.startsWith("/")) {
   479         if (newPath.startsWith(QLatin1Char('/'))) {
   480             finalPath = QValueSpaceSubscriber(newPath).path();
   480             finalPath = QValueSpaceSubscriber(newPath).path();
   481         } else {
   481         } else {
   482             QString oldPath = pwd.path();
   482             QString oldPath = pwd.path();
   483             if(!oldPath.endsWith("/"))
   483             if (!oldPath.endsWith(QLatin1Char('/')))
   484                 oldPath.append("/");
   484                 oldPath.append(QLatin1Char('/'));
   485             oldPath.append(newPath);
   485             oldPath.append(newPath);
   486             oldPath = QDir::cleanPath(oldPath);
   486             oldPath = QDir::cleanPath(oldPath);
   487             finalPath = QValueSpaceSubscriber(oldPath).path();
   487             finalPath = QValueSpaceSubscriber(oldPath).path();
   488         }
   488         }
   489         watch(finalPath.toUtf8());
   489         watch(finalPath);
   490     } else if(cmd == "set" && 3 == cmds.count()) {
   490     } else if (cmd == QLatin1String("set") && 3 == cmds.count()) {
   491         set(cmds.at(1).trimmed(), cmds.at(2).trimmed());
   491         set(cmds.at(1).trimmed(), cmds.at(2).trimmed());
   492     } else if(cmd == "clear" && 2 == cmds.count()) {
   492     } else if (cmd == QLatin1String("clear") && 2 == cmds.count()) {
   493         clear(cmds.at(1).trimmed());
   493         clear(cmds.at(1).trimmed());
   494     } else if((cmd == "subscribe" || cmd == "sub") && 1 == cmds.count()) {
   494     } else if ((cmd == QLatin1String("subscribe") || cmd == QLatin1String("sub")) &&
       
   495                1 == cmds.count()) {
   495         subscribe();
   496         subscribe();
   496     } else if((cmd == "unsubscribe" || cmd == "unsub") && 1 == cmds.count()) {
   497     } else if ((cmd == QLatin1String("unsubscribe") || cmd == QLatin1String("unsub")) &&
       
   498                1 == cmds.count()) {
   497         unsubscribe();
   499         unsubscribe();
   498     } else if((cmd == "?" || cmd == "help") && 1 == cmds.count()) {
   500     } else if ((cmd == QLatin1String("?") || cmd == QLatin1String("help")) && 1 == cmds.count()) {
   499         printHelp();
   501         printHelp();
   500     } else if((cmd == "quit" || cmd == "exit") && 1 == cmds.count()) {
   502     } else if ((cmd == QLatin1String("quit") || cmd == QLatin1String("exit")) &&
       
   503                1 == cmds.count()) {
   501         quit();
   504         quit();
   502     } else if((cmd == "suppress") && 1 == cmds.count()) {
   505     } else if (cmd == QLatin1String("suppress") && 1 == cmds.count()) {
   503         suppress();
   506         suppress();
   504     } else if((cmd == "watchers") && 1 == cmds.count()) {
   507     } else if (cmd == QLatin1String("watchers") && 1 == cmds.count()) {
   505         listwatchers();
   508         listwatchers();
   506     } else if((cmd == "subscriptions") && 1 == cmds.count()) {
   509     } else if (cmd == QLatin1String("subscriptions") && 1 == cmds.count()) {
   507         subscriptions();
   510         subscriptions();
   508     } else if(cmd.isEmpty()) {
       
   509     } else {
   511     } else {
   510         printError();
   512         printError();
   511     }
   513     }
   512 }
   514 }
   513 
   515 
   664             textExt = text.mid(last + 1);
   666             textExt = text.mid(last + 1);
   665         }
   667         }
   666 
   668 
   667         QString vsBase;
   669         QString vsBase;
   668 
   670 
   669         if(*textBase.constData() != '/') {
   671         if (!textBase.startsWith(QLatin1Char('/'))) {
   670             QString in = vse->path();
   672             QString in = vse->path();
   671             if(!in.endsWith("/"))
   673             if (!in.endsWith(QLatin1Char('/')))
   672                 vsBase = in + "/" + textBase;
   674                 vsBase = in + QLatin1Char('/') + textBase;
   673             else
   675             else
   674                 vsBase = in + textBase;
   676                 vsBase = in + textBase;
   675         } else {
   677         } else {
   676             vsBase = textBase;
   678             vsBase = textBase;
   677         }
   679         }
   683         foreach(QString child, schildren) {
   685         foreach(QString child, schildren) {
   684             if(child.startsWith(textExt)) {
   686             if(child.startsWith(textExt)) {
   685                 QString completion;
   687                 QString completion;
   686                 completion.append(textBase);
   688                 completion.append(textBase);
   687                 if(!completion.isEmpty())
   689                 if(!completion.isEmpty())
   688                     completion.append("/");
   690                     completion.append(QLatin1Char('/'));
   689                 completion.append(child.toAscii());
   691                 completion.append(child.toAscii());
   690                 completion.append("/");
   692                 completion.append(QLatin1Char('/'));
   691                 children.append(completion);
   693                 children.append(completion);
   692             }
   694             }
   693         }
   695         }
   694     }
   696     }
   695 
   697 
   806 
   808 
   807     if(manager)
   809     if(manager)
   808         QValueSpace::initValueSpaceServer();
   810         QValueSpace::initValueSpaceServer();
   809 
   811 
   810     if(dump) {
   812     if(dump) {
   811         QValueSpaceSubscriber subscriber("/");
   813         QValueSpaceSubscriber subscriber(QLatin1String("/"));
   812         dodump(&subscriber);
   814         dodump(&subscriber);
   813         return 0;
   815         return 0;
   814     } else {
   816     } else {
   815         vse = new VSExplorer;
   817         vse = new VSExplorer;
   816         LineInput li;
   818         LineInput li;