/**
  * Loads the protocol file.
  */
bool Calculator::openProtocol(QString fileName)
  {
    QFile protocolFile(fileName);
    if(!protocolFile.exists())
      {
         emit errorMessage(tr("File Open Error"), tr("File does not exist:") + fileName );
         return false;
      }
    if(!protocolFile.open(QIODevice::ReadOnly))
      {
         emit errorMessage(tr("File Open Error"), tr("Can not open file:") + fileName );
         return false;
      }
   return true;
  }
Exemple #2
0
int create(KCmdLineArgs *args)
{
    KODE::Printer p;
    if(args->isSet("warning")) p.setCreationWarning(true);

    bool createKioslave = args->isSet("create-kioslave");
    bool createMain = args->isSet("create-main");

    QString filename = args->getOption("filename");

    if(createMain)
    {
        if(filename.isEmpty())
        {
            kdError() << "Error: No file name given." << endl;
            return 1;
        }

        if(filename.endsWith(".cpp"))
        {
            filename = filename.left(filename.length() - 4);
        }
    }
    else
    {
        if(!args->isSet("classname"))
        {
            kdError() << "Error: No class name given." << endl;
            return 1;
        }
    }

    QString className = args->getOption("classname");

    QString protocol;

    if(createKioslave)
    {
        if(!args->isSet("protocol"))
        {
            protocol = className.lower();
            kdWarning() << "Warning: No protocol for kioslave given. Assuming '"
                        << protocol << "'" << endl;
        }
        else
        {
            protocol = args->getOption("protocol");
        }
    }

    KODE::File file;

    file.setProject(args->getOption("project"));

    QString authorEmail = args->getOption("author-email");
    QString authorName;
    KABC::Addressee a;
    if(authorEmail.isEmpty())
    {
        a = KABC::StdAddressBook::self()->whoAmI();
        authorEmail = a.preferredEmail();
    }
    else
    {
        KABC::Addressee::List as =
            KABC::StdAddressBook::self()->findByEmail(authorEmail);
        if(as.isEmpty())
        {
            kdDebug() << "Unable to find '" << authorEmail << "' in address book."
                      << endl;
        }
        else
        {
            a = as.first();
        }
    }
    if(!a.isEmpty())
    {
        authorName = a.realName();
    }
    if(!authorEmail.isEmpty())
    {
        file.addCopyright(QDate::currentDate().year(), authorName, authorEmail);
    }

    KODE::License l;
    if(args->isSet("gpl")) l = KODE::License(KODE::License::GPL);
    if(args->isSet("lgpl")) l = KODE::License(KODE::License::LGPL);
    l.setQtException(args->isSet("qt-exception"));
    file.setLicense(l);

    file.setNameSpace(args->getOption("namespace"));

    if(createMain)
    {
        file.addInclude("kaboutdata.h");
        file.addInclude("kapplication.h");
        file.addInclude("kdebug");
        file.addInclude("klocale");
        file.addInclude("kcmdlineargs");

        KODE::Code code;
        code += "static const KCmdLineOptions options[] =";
        code += "{";
        code += "  { \"verbose\", \"Verbose output\", 0 },";
        code += "  KCmdLineLastOption";
        code += "};";
        file.addFileCode(code);

        KODE::Function main("main", "int");
        main.addArgument("int argc");
        main.addArgument("char **argv");

        code.clear();
        code += "KAboutData aboutData(\"test\",\"Test\",\"0.1\");";
        code += "KCmdLineArgs::init(argc,argv,&aboutData);";
        code += "KCmdLineArgs::addCmdLineOptions( options );";
        code += "";
        code += "KApplication app;";
        code += "";
        code += "KCmdLineArgs *args = KCmdLineArgs::parsedArgs();";
        code += "";
        code += "Q_UNUSED( args );";
        main.setBody(code);

        file.addFileFunction(main);

        file.setFilename(filename);

        p.printImplementation(file, false);

        return 0;
    }

    KODE::Class c(className);

    if(args->isSet("create-dialog"))
    {
        c.addBaseClass(KODE::Class("KDialogBase"));
        c.addInclude("kdialogbase.h");
    }
    else if(createKioslave)
    {
        c.setDocs("This class implements a kioslave for ...");

        c.addBaseClass(KODE::Class("SlaveBase", "KIO"));
        c.addHeaderInclude("kio/slavebase.h");

        KODE::Function get("get", "void");
        get.addArgument("const KURL &url");

        KODE::Code code;

        code += "kdDebug(7000) << \"" + className + "::get()\" << endl;";
        code += "kdDebug(7000) << \" URL: \" << url.url() << endl;";
        code += "#if 1";
        code += "kdDebug(7000) << \" Path: \" << url.path() << endl;";
        code += "kdDebug(7000) << \" Query: \" << url.query() << endl;";
        code += "kdDebug(7000) << \" Protocol: \" << url.protocol() << endl;";
        code += "kdDebug(7000) << \" Filename: \" << url.filename() << endl;";
        code += "#endif";
        code.newLine();

        code += "mimeType( \"text/plain\" );";
        code.newLine();

        code += "QCString str( \"Hello!\" );";
        code += "data( str );";
        code.newLine();

        code += "finished();";
        code.newLine();

        code += "kdDebug(7000) << \"" + className + "CgiProtocol::get() done\" << endl;";

        get.setBody(code);

        c.addFunction(get);


        c.addInclude("kinstance.h");
        c.addInclude("kdebug.h");
        c.addInclude("sys/types.h");
        c.addInclude("unistd.h");
        c.addInclude("stdlib.h");

        KODE::Function main("kdemain", "int");
        main.addArgument("int argc");
        main.addArgument("char **argv");

        code.clear();

        code += "KInstance instance( \"kio_" + protocol + "\" );";
        code += "";
        code += "kdDebug(7000) << \"Starting kio_" + protocol + "(pid:  \" << getpid() << \")\" << endl;";
        code += "";
        code += "if (argc != 4) {";
        code.indent();
        code += "fprintf( stderr, \"Usage: kio_" + protocol + " protocol domain-socket1 domain-socket2\\n\");";
        code += "exit( -1 );";
        code.unindent();
        code += "}";
        code += "";
        code += className + " slave( argv[2], argv[3] );";
        code += "slave.dispatchLoop();";
        code += "";
        code += "return 0;";

        main.setBody(code);

        file.addFileFunction(main);

        file.addExternCDeclaration(p.functionSignature(main));
    }

    KODE::Function constructor(className);

    if(args->isSet("singleton"))
    {
        constructor.setAccess(KODE::Function::Private);

        KODE::Function self("self", className + " *");
        self.setStatic(true);

        KODE::Code code;
        code += "if ( !mSelf ) {";
        code += "  selfDeleter.setObject( mSelf, new " + className + "() );";
        code += "}";
        code += "return mSelf;";

        self.setBody(code);

        c.addFunction(self);

        KODE::MemberVariable selfVar("mSelf", className + " *", true);
        selfVar.setInitializer("0");

        c.addMemberVariable(selfVar);

        KODE::Variable staticDeleter("selfDeleter",
                                     "KStaticDeleter<" + className + ">",
                                     true);
        file.addFileVariable(staticDeleter);
        file.addInclude("kstaticdeleter.h");
    }

    if(createKioslave)
    {
        constructor.addArgument("const QCString &pool");
        constructor.addArgument("const QCString &app");

        constructor.addInitializer("SlaveBase( \"" + protocol + "\", pool, app )");
    }

    c.addFunction(constructor);

    file.insertClass(c);

    p.printHeader(file);
    p.printImplementation(file);

    if(createKioslave)
    {
        // Write automake Makefile
        KODE::AutoMakefile am;

        am.addEntry("INCLUDES", "$(all_includes)");
        am.newLine();
        am.addEntry("noinst_HEADERS", className.lower() + ".h");
        am.newLine();
        am.addEntry("METASOURCES", "AUTO");
        am.newLine();
        am.addEntry("kdelnkdir", "$(kde_servicesdir)");
        am.addEntry("kdelnk_DATA", protocol + ".protocol");

        KODE::AutoMakefile::Target t("kde_module_LTLIBRARIES",
                                     "kio_" + protocol + ".la");
        t.setSources(className.lower() + ".cpp");
        t.setLibAdd("$(LIB_KIO)");
        t.setLdFlags("$(all_libraries) -module $(KDE_PLUGIN)");

        am.addTarget(t);

        p.printAutoMakefile(am);


        // Write protocol file
        QString protocolFilename = protocol + ".protocol";

        QFileInfo fi(protocolFilename);
        protocolFilename = fi.absFilePath();

        KSaveFile::backupFile(protocolFilename, QString::null, ".backup");

        QFile::remove(protocolFilename);

        KSimpleConfig protocolFile(protocolFilename);

        protocolFile.setGroup("Protocol");
        protocolFile.writeEntry("exec", "kio_" + protocol);
        protocolFile.writeEntry("protocol", protocol);
        protocolFile.writeEntry("input", "none");
        protocolFile.writeEntry("output", "filesystem");
        protocolFile.writeEntry("reading", "true");
        protocolFile.writeEntry("DocPath", "kioslave/" + protocol + ".html");

        protocolFile.sync();
    }

    return 0;
}