1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
|
/*
*
* This file is part of the KDE libraries
* Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
**/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <kde_file.h>
#include <QtCore/QDate>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QTextCodec>
#ifdef _WIN32_WCE
#include <QtCore/QDir>
#endif
#include <kconfig.h>
#include <kconfiggroup.h>
#include <kdebug.h>
#include <klocale.h>
#include <kcmdlineargs.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kaboutdata.h>
#include <kcomponentdata.h>
#include <ktemporaryfile.h>
#include <kurl.h>
#include "kconfigutils.h"
class KonfUpdate
{
public:
KonfUpdate();
~KonfUpdate();
QStringList findUpdateFiles(bool dirtyOnly);
QTextStream &log();
QTextStream &logFileError();
bool checkFile(const QString &filename);
void checkGotFile(const QString &_file, const QString &id);
bool updateFile(const QString &filename);
void gotId(const QString &_id);
void gotFile(const QString &_file);
void gotGroup(const QString &_group);
void gotRemoveGroup(const QString &_group);
void gotKey(const QString &_key);
void gotRemoveKey(const QString &_key);
void gotAllKeys();
void gotAllGroups();
void gotOptions(const QString &_options);
void gotScript(const QString &_script);
void gotScriptArguments(const QString &_arguments);
void resetOptions();
void copyGroup(const KConfigBase *cfg1, const QString &group1,
KConfigBase *cfg2, const QString &group2);
void copyGroup(const KConfigGroup &cg1, KConfigGroup &cg2);
void copyOrMoveKey(const QStringList &srcGroupPath, const QString &srcKey, const QStringList &dstGroupPath, const QString &dstKey);
void copyOrMoveGroup(const QStringList &srcGroupPath, const QStringList &dstGroupPath);
QStringList parseGroupString(const QString &_str);
protected:
KConfig *m_config;
QString m_currentFilename;
bool m_skip;
bool m_skipFile;
bool m_debug;
QString m_id;
QString m_oldFile;
QString m_newFile;
QString m_newFileName;
KConfig *m_oldConfig1; // Config to read keys from.
KConfig *m_oldConfig2; // Config to delete keys from.
KConfig *m_newConfig;
QStringList m_oldGroup;
QStringList m_newGroup;
bool m_bCopy;
bool m_bOverwrite;
bool m_bUseConfigInfo;
QString m_arguments;
QTextStream *m_textStream;
QFile *m_file;
QString m_line;
int m_lineCount;
};
KonfUpdate::KonfUpdate()
: m_textStream(0), m_file(0)
{
bool updateAll = false;
m_oldConfig1 = 0;
m_oldConfig2 = 0;
m_newConfig = 0;
m_config = new KConfig("kconf_updaterc");
KConfigGroup cg(m_config, QString());
QStringList updateFiles;
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
m_debug = args->isSet("debug");
m_bUseConfigInfo = false;
if (args->isSet("check")) {
m_bUseConfigInfo = true;
QString file = KStandardDirs::locate("data", "kconf_update/" + args->getOption("check"));
if (file.isEmpty()) {
qWarning("File '%s' not found.", args->getOption("check").toLocal8Bit().data());
log() << "File '" << args->getOption("check") << "' passed on command line not found" << endl;
return;
}
updateFiles.append(file);
} else if (args->count()) {
for (int i = 0; i < args->count(); i++) {
KUrl url = args->url(i);
if (!url.isLocalFile()) {
KCmdLineArgs::usageError(i18n("Only local files are supported."));
}
updateFiles.append(url.toLocalFile());
}
} else {
if (cg.readEntry("autoUpdateDisabled", false))
return;
updateFiles = findUpdateFiles(true);
updateAll = true;
}
for (QStringList::ConstIterator it = updateFiles.constBegin();
it != updateFiles.constEnd();
++it) {
updateFile(*it);
}
if (updateAll && !cg.readEntry("updateInfoAdded", false)) {
cg.writeEntry("updateInfoAdded", true);
updateFiles = findUpdateFiles(false);
for (QStringList::ConstIterator it = updateFiles.constBegin();
it != updateFiles.constEnd();
++it) {
checkFile(*it);
}
updateFiles.clear();
}
}
KonfUpdate::~KonfUpdate()
{
delete m_config;
delete m_file;
delete m_textStream;
}
QTextStream & operator<<(QTextStream & stream, const QStringList & lst)
{
stream << lst.join(", ");
return stream;
}
QTextStream &
KonfUpdate::log()
{
if (!m_textStream) {
QString file = KStandardDirs::locateLocal("data", "kconf_update/log/update.log");
m_file = new QFile(file);
if (m_file->open(QIODevice::WriteOnly | QIODevice::Append)) {
m_textStream = new QTextStream(m_file);
} else {
// Error
m_textStream = new QTextStream(stderr, QIODevice::WriteOnly);
}
}
(*m_textStream) << QDateTime::currentDateTime().toString(Qt::ISODate) << " ";
return *m_textStream;
}
QTextStream &
KonfUpdate::logFileError()
{
return log() << m_currentFilename << ':' << m_lineCount << ":'" << m_line << "': ";
}
QStringList KonfUpdate::findUpdateFiles(bool dirtyOnly)
{
QStringList result;
const QStringList list = KGlobal::dirs()->findAllResources("data", "kconf_update/*.upd",
KStandardDirs::NoDuplicates);
for (QStringList::ConstIterator it = list.constBegin();
it != list.constEnd();
++it) {
QString file = *it;
KDE_struct_stat buff;
if (KDE::stat(file, &buff) == 0) {
int i = file.lastIndexOf('/');
if (i != -1) {
file = file.mid(i + 1);
}
KConfigGroup cg(m_config, file);
time_t ctime = cg.readEntry("ctime", 0);
time_t mtime = cg.readEntry("mtime", 0);
if (!dirtyOnly ||
(ctime != buff.st_ctime) || (mtime != buff.st_mtime)) {
result.append(*it);
}
}
}
return result;
}
bool KonfUpdate::checkFile(const QString &filename)
{
m_currentFilename = filename;
int i = m_currentFilename.lastIndexOf('/');
if (i != -1) {
m_currentFilename = m_currentFilename.mid(i + 1);
}
m_skip = true;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
QTextStream ts(&file);
ts.setCodec(QTextCodec::codecForName("ISO-8859-1"));
int lineCount = 0;
resetOptions();
QString id;
while (!ts.atEnd()) {
QString line = ts.readLine().trimmed();
lineCount++;
if (line.isEmpty() || (line[0] == '#')) {
continue;
}
if (line.startsWith("Id=")) {
id = m_currentFilename + ':' + line.mid(3);
} else if (line.startsWith("File=")) {
checkGotFile(line.mid(5), id);
}
}
return true;
}
void KonfUpdate::checkGotFile(const QString &_file, const QString &id)
{
QString file;
int i = _file.indexOf(',');
if (i == -1) {
file = _file.trimmed();
} else {
file = _file.mid(i + 1).trimmed();
}
// qDebug("File %s, id %s", file.toLatin1().constData(), id.toLatin1().constData());
KConfig cfg(file, KConfig::SimpleConfig);
KConfigGroup cg(&cfg, "$Version");
QStringList ids = cg.readEntry("update_info", QStringList());
if (ids.contains(id)) {
return;
}
ids.append(id);
cg.writeEntry("update_info", ids);
}
/**
* Syntax:
* # Comment
* Id=id
* File=oldfile[,newfile]
* AllGroups
* Group=oldgroup[,newgroup]
* RemoveGroup=oldgroup
* Options=[copy,][overwrite,]
* Key=oldkey[,newkey]
* RemoveKey=ldkey
* AllKeys
* Keys= [Options](AllKeys|(Key|RemoveKey)*)
* ScriptArguments=arguments
* Script=scriptfile[,interpreter]
*
* Sequence:
* (Id,(File(Group,Keys)*)*)*
**/
bool KonfUpdate::updateFile(const QString &filename)
{
m_currentFilename = filename;
int i = m_currentFilename.lastIndexOf('/');
if (i != -1) {
m_currentFilename = m_currentFilename.mid(i + 1);
}
m_skip = true;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
log() << "Checking update-file '" << filename << "' for new updates" << endl;
QTextStream ts(&file);
ts.setCodec(QTextCodec::codecForName("ISO-8859-1"));
m_lineCount = 0;
resetOptions();
while (!ts.atEnd()) {
m_line = ts.readLine().trimmed();
m_lineCount++;
if (m_line.isEmpty() || (m_line[0] == '#')) {
continue;
}
if (m_line.startsWith(QLatin1String("Id="))) {
gotId(m_line.mid(3));
} else if (m_skip) {
continue;
} else if (m_line.startsWith(QLatin1String("Options="))) {
gotOptions(m_line.mid(8));
} else if (m_line.startsWith(QLatin1String("File="))) {
gotFile(m_line.mid(5));
} else if (m_skipFile) {
continue;
} else if (m_line.startsWith(QLatin1String("Group="))) {
gotGroup(m_line.mid(6));
} else if (m_line.startsWith(QLatin1String("RemoveGroup="))) {
gotRemoveGroup(m_line.mid(12));
resetOptions();
} else if (m_line.startsWith(QLatin1String("Script="))) {
gotScript(m_line.mid(7));
resetOptions();
} else if (m_line.startsWith(QLatin1String("ScriptArguments="))) {
gotScriptArguments(m_line.mid(16));
} else if (m_line.startsWith(QLatin1String("Key="))) {
gotKey(m_line.mid(4));
resetOptions();
} else if (m_line.startsWith(QLatin1String("RemoveKey="))) {
gotRemoveKey(m_line.mid(10));
resetOptions();
} else if (m_line == "AllKeys") {
gotAllKeys();
resetOptions();
} else if (m_line == "AllGroups") {
gotAllGroups();
resetOptions();
} else {
logFileError() << "Parse error" << endl;
}
}
// Flush.
gotId(QString());
KDE_struct_stat buff;
if (KDE::stat(filename, &buff) == 0) {
KConfigGroup cg(m_config, m_currentFilename);
cg.writeEntry("ctime", int(buff.st_ctime));
cg.writeEntry("mtime", int(buff.st_mtime));
cg.sync();
}
return true;
}
void KonfUpdate::gotId(const QString &_id)
{
if (!m_id.isEmpty() && !m_skip) {
KConfigGroup cg(m_config, m_currentFilename);
QStringList ids = cg.readEntry("done", QStringList());
if (!ids.contains(m_id)) {
ids.append(m_id);
cg.writeEntry("done", ids);
cg.sync();
}
}
// Flush pending changes
gotFile(QString());
KConfigGroup cg(m_config, m_currentFilename);
QStringList ids = cg.readEntry("done", QStringList());
if (!_id.isEmpty()) {
if (ids.contains(_id)) {
//qDebug("Id '%s' was already in done-list", _id.toLatin1().constData());
if (!m_bUseConfigInfo) {
m_skip = true;
return;
}
}
m_skip = false;
m_skipFile = false;
m_id = _id;
if (m_bUseConfigInfo) {
log() << m_currentFilename << ": Checking update '" << _id << "'" << endl;
} else {
log() << m_currentFilename << ": Found new update '" << _id << "'" << endl;
}
}
}
void KonfUpdate::gotFile(const QString &_file)
{
// Reset group
gotGroup(QString());
if (!m_oldFile.isEmpty()) {
// Close old file.
delete m_oldConfig1;
m_oldConfig1 = 0;
KConfigGroup cg(m_oldConfig2, "$Version");
QStringList ids = cg.readEntry("update_info", QStringList());
QString cfg_id = m_currentFilename + ':' + m_id;
if (!ids.contains(cfg_id) && !m_skip) {
ids.append(cfg_id);
cg.writeEntry("update_info", ids);
}
cg.sync();
delete m_oldConfig2;
m_oldConfig2 = 0;
QString file = KStandardDirs::locateLocal("config", m_oldFile);
KDE_struct_stat s_buf;
if (KDE::stat(file, &s_buf) == 0) {
if (s_buf.st_size == 0) {
// Delete empty file.
QFile::remove(file);
}
}
m_oldFile.clear();
}
if (!m_newFile.isEmpty()) {
// Close new file.
KConfigGroup cg(m_newConfig, "$Version");
QStringList ids = cg.readEntry("update_info", QStringList());
QString cfg_id = m_currentFilename + ':' + m_id;
if (!ids.contains(cfg_id) && !m_skip) {
ids.append(cfg_id);
cg.writeEntry("update_info", ids);
}
m_newConfig->sync();
delete m_newConfig;
m_newConfig = 0;
m_newFile.clear();
}
m_newConfig = 0;
int i = _file.indexOf(',');
if (i == -1) {
m_oldFile = _file.trimmed();
} else {
m_oldFile = _file.left(i).trimmed();
m_newFile = _file.mid(i + 1).trimmed();
if (m_oldFile == m_newFile) {
m_newFile.clear();
}
}
if (!m_oldFile.isEmpty()) {
m_oldConfig2 = new KConfig(m_oldFile, KConfig::NoGlobals);
QString cfg_id = m_currentFilename + ':' + m_id;
KConfigGroup cg(m_oldConfig2, "$Version");
QStringList ids = cg.readEntry("update_info", QStringList());
if (ids.contains(cfg_id)) {
m_skip = true;
m_newFile.clear();
log() << m_currentFilename << ": Skipping update '" << m_id << "'" << endl;
}
if (!m_newFile.isEmpty()) {
m_newConfig = new KConfig(m_newFile, KConfig::NoGlobals);
KConfigGroup cg(m_newConfig, "$Version");
ids = cg.readEntry("update_info", QStringList());
if (ids.contains(cfg_id)) {
m_skip = true;
log() << m_currentFilename << ": Skipping update '" << m_id << "'" << endl;
}
} else {
m_newConfig = m_oldConfig2;
}
m_oldConfig1 = new KConfig(m_oldFile, KConfig::NoGlobals);
} else {
m_newFile.clear();
}
m_newFileName = m_newFile;
if (m_newFileName.isEmpty()) {
m_newFileName = m_oldFile;
}
m_skipFile = false;
if (!m_oldFile.isEmpty()) { // if File= is specified, it doesn't exist, is empty or contains only kconf_update's [$Version] group, skip
if (m_oldConfig1 != NULL
&& (m_oldConfig1->groupList().isEmpty()
|| (m_oldConfig1->groupList().count() == 1 && m_oldConfig1->groupList().first() == "$Version"))) {
log() << m_currentFilename << ": File '" << m_oldFile << "' does not exist or empty, skipping" << endl;
m_skipFile = true;
}
}
}
QStringList KonfUpdate::parseGroupString(const QString &str)
{
bool ok;
QString error;
QStringList lst = KConfigUtils::parseGroupString(str, &ok, &error);
if (!ok) {
logFileError() << error;
}
return lst;
}
void KonfUpdate::gotGroup(const QString &_group)
{
QString group = _group.trimmed();
if (group.isEmpty()) {
m_oldGroup = m_newGroup = QStringList();
return;
}
QStringList tokens = group.split(',');
m_oldGroup = parseGroupString(tokens.at(0));
if (tokens.count() == 1) {
m_newGroup = m_oldGroup;
} else {
m_newGroup = parseGroupString(tokens.at(1));
}
}
void KonfUpdate::gotRemoveGroup(const QString &_group)
{
m_oldGroup = parseGroupString(_group);
if (!m_oldConfig1) {
logFileError() << "RemoveGroup without previous File specification" << endl;
return;
}
KConfigGroup cg = KConfigUtils::openGroup(m_oldConfig2, m_oldGroup);
if (!cg.exists()) {
return;
}
// Delete group.
cg.deleteGroup();
log() << m_currentFilename << ": RemoveGroup removes group " << m_oldFile << ":" << m_oldGroup << endl;
}
void KonfUpdate::gotKey(const QString &_key)
{
QString oldKey, newKey;
int i = _key.indexOf(',');
if (i == -1) {
oldKey = _key.trimmed();
newKey = oldKey;
} else {
oldKey = _key.left(i).trimmed();
newKey = _key.mid(i + 1).trimmed();
}
if (oldKey.isEmpty() || newKey.isEmpty()) {
logFileError() << "Key specifies invalid key" << endl;
return;
}
if (!m_oldConfig1) {
logFileError() << "Key without previous File specification" << endl;
return;
}
copyOrMoveKey(m_oldGroup, oldKey, m_newGroup, newKey);
}
void KonfUpdate::copyOrMoveKey(const QStringList &srcGroupPath, const QString &srcKey, const QStringList &dstGroupPath, const QString &dstKey)
{
KConfigGroup dstCg = KConfigUtils::openGroup(m_newConfig, dstGroupPath);
if (!m_bOverwrite && dstCg.hasKey(dstKey)) {
log() << m_currentFilename << ": Skipping " << m_newFileName << ":" << dstCg.name() << ":" << dstKey << ", already exists." << endl;
return;
}
KConfigGroup srcCg = KConfigUtils::openGroup(m_oldConfig1, srcGroupPath);
if (!srcCg.hasKey(srcKey))
return;
QString value = srcCg.readEntry(srcKey, QString());
log() << m_currentFilename << ": Updating " << m_newFileName << ":" << dstCg.name() << ":" << dstKey << " to '" << value << "'" << endl;
dstCg.writeEntry(dstKey, value);
if (m_bCopy) {
return; // Done.
}
// Delete old entry
if (m_oldConfig2 == m_newConfig
&& srcGroupPath == dstGroupPath
&& srcKey == dstKey) {
return; // Don't delete!
}
KConfigGroup srcCg2 = KConfigUtils::openGroup(m_oldConfig2, srcGroupPath);
srcCg2.deleteEntry(srcKey);
log() << m_currentFilename << ": Removing " << m_oldFile << ":" << srcCg2.name() << ":" << srcKey << ", moved." << endl;
}
void KonfUpdate::copyOrMoveGroup(const QStringList &srcGroupPath, const QStringList &dstGroupPath)
{
KConfigGroup cg = KConfigUtils::openGroup(m_oldConfig1, srcGroupPath);
// Keys
Q_FOREACH(const QString &key, cg.keyList()) {
copyOrMoveKey(srcGroupPath, key, dstGroupPath, key);
}
// Subgroups
Q_FOREACH(const QString &group, cg.groupList()) {
QStringList groupPath = QStringList() << group;
copyOrMoveGroup(srcGroupPath + groupPath, dstGroupPath + groupPath);
}
}
void KonfUpdate::gotRemoveKey(const QString &_key)
{
QString key = _key.trimmed();
if (key.isEmpty()) {
logFileError() << "RemoveKey specifies invalid key" << endl;
return;
}
if (!m_oldConfig1) {
logFileError() << "Key without previous File specification" << endl;
return;
}
KConfigGroup cg1 = KConfigUtils::openGroup(m_oldConfig1, m_oldGroup);
if (!cg1.hasKey(key)) {
return;
}
log() << m_currentFilename << ": RemoveKey removes " << m_oldFile << ":" << m_oldGroup << ":" << key << endl;
// Delete old entry
KConfigGroup cg2 = KConfigUtils::openGroup(m_oldConfig2, m_oldGroup);
cg2.deleteEntry(key);
/*if (m_oldConfig2->deleteGroup(m_oldGroup, KConfig::Normal)) { // Delete group if empty.
log() << m_currentFilename << ": Removing empty group " << m_oldFile << ":" << m_oldGroup << endl;
} (this should be automatic)*/
}
void KonfUpdate::gotAllKeys()
{
if (!m_oldConfig1) {
logFileError() << "AllKeys without previous File specification" << endl;
return;
}
copyOrMoveGroup(m_oldGroup, m_newGroup);
}
void KonfUpdate::gotAllGroups()
{
if (!m_oldConfig1) {
logFileError() << "AllGroups without previous File specification" << endl;
return;
}
const QStringList allGroups = m_oldConfig1->groupList();
for (QStringList::ConstIterator it = allGroups.begin();
it != allGroups.end(); ++it) {
m_oldGroup = QStringList() << *it;
m_newGroup = m_oldGroup;
gotAllKeys();
}
}
void KonfUpdate::gotOptions(const QString &_options)
{
const QStringList options = _options.split(',');
for (QStringList::ConstIterator it = options.begin();
it != options.end();
++it) {
if ((*it).toLower().trimmed() == "copy") {
m_bCopy = true;
}
if ((*it).toLower().trimmed() == "overwrite") {
m_bOverwrite = true;
}
}
}
void KonfUpdate::copyGroup(const KConfigBase *cfg1, const QString &group1,
KConfigBase *cfg2, const QString &group2)
{
KConfigGroup cg1(cfg1, group1);
KConfigGroup cg2(cfg2, group2);
copyGroup(cg1, cg2);
}
void KonfUpdate::copyGroup(const KConfigGroup &cg1, KConfigGroup &cg2)
{
// Copy keys
QMap<QString, QString> list = cg1.entryMap();
for (QMap<QString, QString>::ConstIterator it = list.constBegin();
it != list.constEnd(); ++it) {
if (m_bOverwrite || !cg2.hasKey(it.key())) {
cg2.writeEntry(it.key(), it.value());
}
}
// Copy subgroups
Q_FOREACH(const QString &group, cg1.groupList()) {
copyGroup(&cg1, group, &cg2, group);
}
}
void KonfUpdate::gotScriptArguments(const QString &_arguments)
{
m_arguments = _arguments;
}
void KonfUpdate::gotScript(const QString &_script)
{
QString script, interpreter;
int i = _script.indexOf(',');
if (i == -1) {
script = _script.trimmed();
} else {
script = _script.left(i).trimmed();
interpreter = _script.mid(i + 1).trimmed();
}
if (script.isEmpty()) {
logFileError() << "Script fails to specify filename";
m_skip = true;
return;
}
QString path = KStandardDirs::locate("data", "kconf_update/" + script);
if (path.isEmpty()) {
if (interpreter.isEmpty()) {
path = KStandardDirs::locate("lib", "kconf_update_bin/" + script);
}
if (path.isEmpty()) {
logFileError() << "Script '" << script << "' not found" << endl;
m_skip = true;
return;
}
}
if (!m_arguments.isNull()) {
log() << m_currentFilename << ": Running script '" << script << "' with arguments '" << m_arguments << "'" << endl;
} else {
log() << m_currentFilename << ": Running script '" << script << "'" << endl;
}
QString cmd;
if (interpreter.isEmpty()) {
cmd = path;
} else {
cmd = interpreter + ' ' + path;
}
if (!m_arguments.isNull()) {
cmd += ' ';
cmd += m_arguments;
}
KTemporaryFile scriptIn;
scriptIn.open();
KTemporaryFile scriptOut;
scriptOut.open();
KTemporaryFile scriptErr;
scriptErr.open();
int result;
if (m_oldConfig1) {
if (m_debug) {
scriptIn.setAutoRemove(false);
log() << "Script input stored in " << scriptIn.fileName() << endl;
}
KConfig cfg(scriptIn.fileName(), KConfig::SimpleConfig);
if (m_oldGroup.isEmpty()) {
// Write all entries to tmpFile;
const QStringList grpList = m_oldConfig1->groupList();
for (QStringList::ConstIterator it = grpList.begin();
it != grpList.end();
++it) {
copyGroup(m_oldConfig1, *it, &cfg, *it);
}
} else {
KConfigGroup cg1 = KConfigUtils::openGroup(m_oldConfig1, m_oldGroup);
KConfigGroup cg2(&cfg, QString());
copyGroup(cg1, cg2);
}
cfg.sync();
#ifndef _WIN32_WCE
result = system(QFile::encodeName(QString("%1 < %2 > %3 2> %4").arg(cmd, scriptIn.fileName(), scriptOut.fileName(), scriptErr.fileName())));
#else
QString path_ = QDir::convertSeparators ( QFileInfo ( cmd ).absoluteFilePath() );
QString file_ = QFileInfo ( cmd ).fileName();
SHELLEXECUTEINFO execInfo;
memset ( &execInfo,0,sizeof ( execInfo ) );
execInfo.cbSize = sizeof ( execInfo );
execInfo.fMask = SEE_MASK_FLAG_NO_UI;
execInfo.lpVerb = L"open";
execInfo.lpFile = (LPCWSTR) path_.utf16();
execInfo.lpDirectory = (LPCWSTR) file_.utf16();
execInfo.lpParameters = (LPCWSTR) QString(" < %1 > %2 2> %3").arg( scriptIn.fileName(), scriptOut.fileName(), scriptErr.fileName()).utf16();
result = ShellExecuteEx ( &execInfo );
if (result != 0)
{
result = 0;
}
else
{
result = -1;
}
#endif
} else {
// No config file
#ifndef _WIN32_WCE
result = system(QFile::encodeName(QString("%1 2> %2").arg(cmd, scriptErr.fileName())));
#else
QString path_ = QDir::convertSeparators ( QFileInfo ( cmd ).absoluteFilePath() );
QString file_ = QFileInfo ( cmd ).fileName();
SHELLEXECUTEINFO execInfo;
memset ( &execInfo,0,sizeof ( execInfo ) );
execInfo.cbSize = sizeof ( execInfo );
execInfo.fMask = SEE_MASK_FLAG_NO_UI;
execInfo.lpVerb = L"open";
execInfo.lpFile = (LPCWSTR) path_.utf16();
execInfo.lpDirectory = (LPCWSTR) file_.utf16();
execInfo.lpParameters = (LPCWSTR) QString(" 2> %1").arg( scriptErr.fileName()).utf16();
result = ShellExecuteEx ( &execInfo );
if (result != 0)
{
result = 0;
}
else
{
result = -1;
}
#endif
}
// Copy script stderr to log file
{
QFile output(scriptErr.fileName());
if (output.open(QIODevice::ReadOnly)) {
QTextStream ts(&output);
ts.setCodec(QTextCodec::codecForName("UTF-8"));
while (!ts.atEnd()) {
QString line = ts.readLine();
log() << "[Script] " << line << endl;
}
}
}
if (result) {
log() << m_currentFilename << ": !! An error occurred while running '" << cmd << "'" << endl;
return;
}
if (!m_oldConfig1) {
return; // Nothing to merge
}
if (m_debug) {
scriptOut.setAutoRemove(false);
log() << "Script output stored in " << scriptOut.fileName() << endl;
}
// Deleting old entries
{
QStringList group = m_oldGroup;
QFile output(scriptOut.fileName());
if (output.open(QIODevice::ReadOnly)) {
QTextStream ts(&output);
ts.setCodec(QTextCodec::codecForName("UTF-8"));
while (!ts.atEnd()) {
QString line = ts.readLine();
if (line.startsWith('[')) {
group = parseGroupString(line);
} else if (line.startsWith(QLatin1String("# DELETE "))) {
QString key = line.mid(9);
if (key[0] == '[') {
int j = key.lastIndexOf(']') + 1;
if (j > 0) {
group = parseGroupString(key.left(j));
key = key.mid(j);
}
}
KConfigGroup cg = KConfigUtils::openGroup(m_oldConfig2, group);
cg.deleteEntry(key);
log() << m_currentFilename << ": Script removes " << m_oldFile << ":" << group << ":" << key << endl;
/*if (m_oldConfig2->deleteGroup(group, KConfig::Normal)) { // Delete group if empty.
log() << m_currentFilename << ": Removing empty group " << m_oldFile << ":" << group << endl;
} (this should be automatic)*/
} else if (line.startsWith(QLatin1String("# DELETEGROUP"))) {
QString str = line.mid(13).trimmed();
if (!str.isEmpty()) {
group = parseGroupString(str);
}
KConfigGroup cg = KConfigUtils::openGroup(m_oldConfig2, group);
cg.deleteGroup();
log() << m_currentFilename << ": Script removes group " << m_oldFile << ":" << group << endl;
}
}
}
}
// Merging in new entries.
KConfig scriptOutConfig(scriptOut.fileName(), KConfig::NoGlobals);
if (m_newGroup.isEmpty()) {
// Copy "default" keys as members of "default" keys
copyGroup(&scriptOutConfig, QString(), m_newConfig, QString());
} else {
// Copy default keys as members of m_newGroup
KConfigGroup srcCg = KConfigUtils::openGroup(&scriptOutConfig, QStringList());
KConfigGroup dstCg = KConfigUtils::openGroup(m_newConfig, m_newGroup);
copyGroup(srcCg, dstCg);
}
Q_FOREACH(const QString &group, scriptOutConfig.groupList()) {
copyGroup(&scriptOutConfig, group, m_newConfig, group);
}
}
void KonfUpdate::resetOptions()
{
m_bCopy = false;
m_bOverwrite = false;
m_arguments.clear();
}
extern "C" KDE_EXPORT int kdemain(int argc, char **argv)
{
KCmdLineOptions options;
options.add("debug", ki18n("Keep output results from scripts"));
options.add("check <update-file>", ki18n("Check whether config file itself requires updating"));
options.add("+[file]", ki18n("File to read update instructions from"));
KAboutData aboutData("kconf_update", 0, ki18n("KConf Update"),
"1.0.2",
ki18n("KDE Tool for updating user configuration files"),
KAboutData::License_GPL,
ki18n("(c) 2001, Waldo Bastian"));
aboutData.addAuthor(ki18n("Waldo Bastian"), KLocalizedString(), "bastian@kde.org");
KCmdLineArgs::init(argc, argv, &aboutData);
KCmdLineArgs::addCmdLineOptions(options);
KComponentData componentData(&aboutData);
KonfUpdate konfUpdate;
return 0;
}
|