qthighway/xqsreg/main.cpp
branchRCL_3
changeset 10 cd2778e5acfe
parent 9 5d007b20cfd0
child 11 19a54be74e5e
equal deleted inserted replaced
9:5d007b20cfd0 10:cd2778e5acfe
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 #include <QString>
       
    23 #include <QStringList>
       
    24 #include <QDebug>
       
    25 #include <QFile>
       
    26 #include <QIODevice>
       
    27 #include <QTextStream>
       
    28 #include <QDateTime>
       
    29 #include <QDir>
       
    30 #include <QFileInfo>
       
    31 
       
    32 #include <QRegExp>
       
    33 
       
    34 #include <iostream>
       
    35 
       
    36 using namespace std;
       
    37 
       
    38 #define RESOURCE_DIRECTORY_RESOURCE "\\\\resource\\\\apps\\\\"
       
    39 #define _s(a) QString::fromLatin1(a)
       
    40 const char *FINGERPRINT = "/* xqsreg.exe fingerprint */";
       
    41 
       
    42 QString appName;
       
    43 QString uid3;
       
    44 QString configurationFileName;
       
    45 QString configurationFile;
       
    46 QString resourceFileName;
       
    47 QString embeddable;
       
    48 QString hidden;
       
    49 
       
    50 QString epocRoot()
       
    51 {
       
    52      QString test=QDir::root().absolutePath() ;
       
    53      qDebug() << QString::fromLatin1("epocroot=") + test;
       
    54      return test;
       
    55 }
       
    56 
       
    57 QString canonizePath(const QString& origPath)
       
    58 {
       
    59     // Since current path gets appended almost always anyway, use it as default
       
    60     // for nonexisting paths.
       
    61     static QString defaultPath;
       
    62     if (defaultPath.isEmpty()) {
       
    63         QFileInfo fi(QString::fromLatin1("."));
       
    64         defaultPath = fi.canonicalFilePath();
       
    65     }
       
    66 
       
    67     // Prepend epocroot to any paths beginning with "/epoc32/"
       
    68     QString resultPath = QDir::fromNativeSeparators(origPath);
       
    69     if (resultPath.startsWith(QString::fromLatin1("/epoc32/"), Qt::CaseInsensitive))
       
    70         resultPath = QDir::fromNativeSeparators(epocRoot()) + resultPath.mid(1);
       
    71 
       
    72     QFileInfo fi(resultPath);
       
    73     if(fi.isDir()) {
       
    74         resultPath = fi.canonicalFilePath();
       
    75     } else {
       
    76         resultPath = fi.canonicalPath();
       
    77     }
       
    78 
       
    79     resultPath = QDir::cleanPath(resultPath);
       
    80 
       
    81     if (resultPath.isEmpty())
       
    82         resultPath = defaultPath;
       
    83 
       
    84     return resultPath;
       
    85 }
       
    86 
       
    87 
       
    88 bool checkIfPatched(const QString &rssContent)
       
    89 {
       
    90 	return rssContent.contains(_s(FINGERPRINT));
       
    91 }
       
    92 
       
    93 int parseProperties(const QStringList &params) 
       
    94 {
       
    95 	int paramCount = params.size();
       
    96 	
       
    97     if (paramCount < 5) {
       
    98         fprintf(stderr, "Warning: invalid parameters!\n");
       
    99         fprintf(stderr, "Usage: xqsreg <application name> <application uid> <conf_file.xml> <resource file name> [embeddable] [hidden]");
       
   100         return 1;
       
   101     }
       
   102 
       
   103 	appName = params[1];
       
   104     uid3=params[2];
       
   105     configurationFileName=params[3];
       
   106     configurationFile = QDir::fromNativeSeparators(canonizePath(QString::fromLatin1(".")) + QString::fromLatin1("/") + configurationFileName);
       
   107     resourceFileName=params[4];
       
   108 	
       
   109 	embeddable = QString::fromLatin1("KAppNotEmbeddable");
       
   110     hidden = QString::fromLatin1("KAppNotHidden");
       
   111 
       
   112     if (paramCount >= 6) {
       
   113 		if(!params[5].compare(QString::fromLatin1("embeddable"), Qt::CaseInsensitive)) {
       
   114 			embeddable = QString::fromLatin1("KAppEmbeddable");
       
   115 
       
   116 		} else if(!params[5].compare(QString::fromLatin1("hidden"), Qt::CaseInsensitive)) {
       
   117             hidden = QString::fromLatin1("KAppIsHidden"); 
       
   118         }
       
   119     }
       
   120     
       
   121 	if (paramCount == 7) {
       
   122         if(!params[6].compare(QString::fromLatin1("embeddable"), Qt::CaseInsensitive)) {
       
   123             embeddable = QString::fromLatin1("KAppEmbeddable");
       
   124         } else if(!params[6].compare(QString::fromLatin1("hidden"), Qt::CaseInsensitive)) {
       
   125             hidden = QString::fromLatin1("KAppIsHidden"); 
       
   126         }
       
   127     }	
       
   128 	
       
   129 	return 0;
       
   130 }
       
   131 
       
   132 int populateBeforeRegInfo(QTextStream& t) 
       
   133 {
       
   134 	t << endl << endl;
       
   135 	t << FINGERPRINT << endl;
       
   136 /*    t << "// ============================================================================" << endl;
       
   137     t << "// * Generated by xqrs on " << QDateTime::currentDateTime().toString() << endl;
       
   138     t << "// * This file is generated by xqrs and should not be modified by the" << endl;
       
   139     t << "// * user." << endl;
       
   140 	t << "// ============================================================================" << endl;
       
   141     t << endl; */
       
   142 	
       
   143 	t << "#include <xqserviceipcconst.h>" << endl;
       
   144 	t << "STRUCT SERVICE_CONFIGURATION {LTEXT xmldata;}" << endl;
       
   145 	t << "STRUCT SERVICE_CONFIGURATION_ARRAY {STRUCT service_configuration_array[];}" << endl;
       
   146 
       
   147 	t << endl;
       
   148 
       
   149 /*	t << "UID2 " << "KUidAppRegistrationResourceFile" << endl;
       
   150 	t << "UID3 " << uid3 << endl << endl; */
       
   151 
       
   152 	return 0;
       
   153 }
       
   154 
       
   155 int populateInsideRegInfo(QTextStream& t) 
       
   156 {
       
   157 	t << endl << endl;
       
   158 /*	t << "\tapp_file=\"" << appName << "\";" << endl;
       
   159 	t << "\tlocalisable_resource_file=\"" RESOURCE_DIRECTORY_RESOURCE << appName << "\";" << endl;
       
   160 	t << endl;*/
       
   161 
       
   162 	t << "\t" << "embeddability   = " << embeddable << ";" <<  endl;
       
   163 	t << "\t" << "hidden          = " << hidden << ";" <<  endl;
       
   164 	t << "\t" << "newfile         = KAppDoesNotSupportNewFile;" <<  endl;
       
   165 	t << "\t" << "service_list =" <<  endl;
       
   166 	t << "\t\t" << "{" <<  endl;
       
   167 	t << "\t\t" << "SERVICE_INFO" <<  endl;
       
   168 	t << "\t\t\t" << "{" <<  endl;
       
   169 	t << "\t\t\t" << "uid = KXQServiceUid;" <<  endl;
       
   170 	t << "\t\t\t" << "datatype_list = {};" <<  endl;
       
   171 	t << "\t\t\t" << "opaque_data = r_service_configuration_reg;" <<  endl;
       
   172 	t << "\t\t\t" << "}" <<  endl;
       
   173 	t << "\t\t" << "};" <<  endl;
       
   174 			
       
   175 	return 0;
       
   176 }
       
   177 
       
   178 int populateAfterRegInfo(QTextStream& t) 
       
   179 {
       
   180 	t << endl << endl;
       
   181 	t << "RESOURCE SERVICE_CONFIGURATION_ARRAY r_service_configuration_reg" << endl;
       
   182 	t << "\t{" << endl;
       
   183 	t << "\t\tservice_configuration_array=" << endl;
       
   184 	t << "\t\t\t{" << endl;
       
   185 
       
   186     QFile cf(configurationFile);	
       
   187 
       
   188 	int err = 0;
       
   189 	
       
   190 	QByteArray escapedQuotationMark = QByteArray("\\\"");
       
   191 	
       
   192 	if (cf.open(QIODevice::ReadOnly | QIODevice::Text)) {
       
   193 		QByteArray xmlConf;
       
   194 
       
   195 		xmlConf = cf.readAll();
       
   196 		xmlConf = xmlConf.replace("\"","\\\"");
       
   197 		if (xmlConf.count()) {
       
   198 			QByteArray xml = xmlConf.simplified();
       
   199 			for (int n=0;;n++) {
       
   200 				int splitCount = 255;
       
   201 				if (xml.size() > 255 && (xml.mid(254, 2) == escapedQuotationMark)) {
       
   202 					splitCount = 254;
       
   203 				}
       
   204 				QByteArray split = xml.left(splitCount);
       
   205 				if (!split.count()) {
       
   206 					break;
       
   207 				}
       
   208 				if (n) {
       
   209 					t << "\t\t\t\t,"  << endl;
       
   210 				}
       
   211 				t << "\t\t\t\tSERVICE_CONFIGURATION"  << endl;
       
   212 				t << "\t\t\t\t{" <<  endl;
       
   213 				t << "\t\t\t\txmldata = \"" << split <<"\";" <<  endl;
       
   214 				t << "\t\t\t\t}" <<  endl;
       
   215 
       
   216 				xml = xml.mid(splitCount);
       
   217 			}
       
   218 		}	
       
   219 	} else {
       
   220 		fprintf(stderr, "Error: Cannot open %s file for reading.", qPrintable(configurationFile));
       
   221 		err = 1;
       
   222 	}
       
   223 	
       
   224 	t << endl;
       
   225 	t << "\t\t\t};" << endl;
       
   226 	t << "\t}" << endl;
       
   227 	
       
   228 	return err;
       
   229 }
       
   230 
       
   231 int runXQSRegGenerator(QStringList params)
       
   232 {
       
   233 	QFile ftR(resourceFileName);
       
   234 	QString fileContent(_s(""));
       
   235 	
       
   236 	if ( ftR.open(QIODevice::ReadOnly | QIODevice::Text) ) {       
       
   237 	    QTextStream t(&ftR);
       
   238 		fileContent = t.readAll(); // let's hope file won't be too big ;)
       
   239 	    ftR.close();
       
   240     } else {
       
   241         fprintf(stderr, "Error: file %s not found.\n", qPrintable(resourceFileName));
       
   242         return 1;
       
   243 	}
       
   244 	
       
   245 	if (checkIfPatched(fileContent)) {
       
   246         fprintf(stderr, "Warning: matched xqsreg.exe fingerprint in resource file %s. Skipping rewriting action.\n", qPrintable(configurationFile));
       
   247         return 0;
       
   248 	}
       
   249 
       
   250 	QRegExp qr(_s("RESOURCE\\s+APP_REGISTRATION_INFO\\s+[{]"));
       
   251 
       
   252 	int pos = qr.indexIn(fileContent, 0);
       
   253 
       
   254 	QString beforeRegistrationInfo;
       
   255 	QString insideRegistrationInfo;
       
   256 	QString afterRegistrationInfo;
       
   257 	
       
   258 	QTextStream tsBeforeRegistrationInfo(&beforeRegistrationInfo);
       
   259 	QTextStream tsInsideRegistrationInfo(&insideRegistrationInfo);
       
   260 	QTextStream tsAfterRegistrationInfo(&afterRegistrationInfo);
       
   261 	
       
   262 	if (populateBeforeRegInfo(tsBeforeRegistrationInfo)) {
       
   263 		return 1;
       
   264 	}
       
   265 		
       
   266 	if (populateInsideRegInfo(tsInsideRegistrationInfo)) {
       
   267 		return 1;
       
   268 	}
       
   269 
       
   270 	if (populateAfterRegInfo(tsAfterRegistrationInfo)) {
       
   271 		return 1;
       
   272 	}
       
   273 	
       
   274 	if (pos != -1) {
       
   275 		fileContent.insert(pos, beforeRegistrationInfo);
       
   276 		pos += qr.matchedLength() + beforeRegistrationInfo.size();
       
   277 		fileContent.insert(pos, insideRegistrationInfo);
       
   278 		fileContent.append(afterRegistrationInfo);
       
   279 	} else {
       
   280         fprintf(stderr, "Error: RESOURCE APP_REGISTRATION_INFO not found in: %s.", qPrintable(configurationFile));
       
   281         return 1;
       
   282 	}
       
   283 	
       
   284     QFile ft(resourceFileName);
       
   285     if(ft.open(QIODevice::WriteOnly)) {
       
   286         QTextStream t(&ft);
       
   287 		t << fileContent;
       
   288 	} else {
       
   289         fprintf(stderr, "Error: cannot open file %s for writing.", qPrintable(configurationFile));
       
   290         return 1;
       
   291 	}
       
   292 	ft.close();
       
   293 
       
   294     return 0;
       
   295 }
       
   296 
       
   297 int main(int argc, char *argv[])
       
   298 {
       
   299     QStringList params;
       
   300     for (int i=0 ; i < argc ; i++) {
       
   301         params << QString::fromLatin1(argv[i]);
       
   302         //printf("par%d=%s\n",i,argv[i]);
       
   303     }
       
   304 	
       
   305 	int err = parseProperties(params);
       
   306 	
       
   307 	if (err == 0) {
       
   308 		err = runXQSRegGenerator(params);
       
   309 	}
       
   310 	return err;
       
   311 }