void Archive::importArchivedBackgroundImages(const QString &extractionFolder) { FormatImporter copier; // Only used to copy files synchronously QString destFolder = KGlobal::dirs()->saveLocation("data", "basket/backgrounds/"); QDir dir(extractionFolder + "backgrounds/", /*nameFilder=*/"*.png", /*sortSpec=*/QDir::Name | QDir::IgnoreCase, /*filterSpec=*/QDir::Files | QDir::NoSymLinks); QStringList files = dir.entryList(); for (QStringList::Iterator it = files.begin(); it != files.end(); ++it) { QString image = *it; if (!Global::backgroundManager->exists(image)) { // Copy images: QString imageSource = extractionFolder + "backgrounds/" + image; QString imageDest = destFolder + image; copier.copyFolder(imageSource, imageDest); // Copy configuration file: QString configSource = extractionFolder + "backgrounds/" + image + ".config"; QString configDest = destFolder + image; if (dir.exists(configSource)) copier.copyFolder(configSource, configDest); // Copy preview: QString previewSource = extractionFolder + "backgrounds/previews/" + image; QString previewDest = destFolder + "previews/" + image; if (dir.exists(previewSource)) { dir.mkdir(destFolder + "previews/"); // Make sure the folder exists! copier.copyFolder(previewSource, previewDest); } // Append image to database: Global::backgroundManager->addImage(imageDest); } } }
void Archive::importBasketIcon(QDomElement properties, const QString &extractionFolder) { QString iconName = XMLWork::getElementText(properties, "icon"); if (!iconName.isEmpty() && iconName != "basket") { QPixmap icon = KIconLoader::global()->loadIcon( iconName, KIconLoader::NoGroup, 16, KIconLoader::DefaultState, QStringList(), 0L, /*canReturnNull=*/true ); // The icon does not exists on that computer, import it: if (icon.isNull()) { QDir dir; dir.mkdir(Global::savesFolder() + "basket-icons/"); FormatImporter copier; // Only used to copy files synchronously // Of the icon path was eg. "/home/seb/icon.png", it was exported as "basket-icons/_home_seb_icon.png". // So we need to copy that image to "~/.kde/share/apps/basket/basket-icons/icon.png": int slashIndex = iconName.lastIndexOf('/'); QString iconFileName = (slashIndex < 0 ? iconName : iconName.right(slashIndex - 2)); QString source = extractionFolder + "basket-icons/" + iconName.replace('/', '_'); QString destination = Global::savesFolder() + "basket-icons/" + iconFileName; if (!dir.exists(destination)) copier.copyFolder(source, destination); // Replace the emblem path in the tags.xml copy: QDomElement iconElement = XMLWork::getElement(properties, "icon"); properties.removeChild(iconElement); QDomDocument document = properties.ownerDocument(); XMLWork::addElement(document, properties, "icon", destination); } } }
/** * When opening a basket archive that come from another computer, * it can contains tags that use icons (emblems) that are not present on that computer. * Fortunately, basket archives contains a copy of every used icons. * This method check for every emblems and import the missing ones. * It also modify the tags.xml copy for the emblems to point to the absolute path of the impported icons. */ void Archive::importTagEmblems(const QString &extractionFolder) { QDomDocument *document = XMLWork::openFile("basketTags", extractionFolder + "tags.xml"); if (document == 0) return; QDomElement docElem = document->documentElement(); QDir dir; dir.mkdir(Global::savesFolder() + "tag-emblems/"); FormatImporter copier; // Only used to copy files synchronously QDomNode node = docElem.firstChild(); while (!node.isNull()) { QDomElement element = node.toElement(); if ( (!element.isNull()) && element.tagName() == "tag" ) { QDomNode subNode = element.firstChild(); while (!subNode.isNull()) { QDomElement subElement = subNode.toElement(); if ( (!subElement.isNull()) && subElement.tagName() == "state" ) { QString emblemName = XMLWork::getElementText(subElement, "emblem"); if (!emblemName.isEmpty()) { QPixmap emblem = KIconLoader::global()->loadIcon(emblemName, KIconLoader::NoGroup, 16, KIconLoader::DefaultState, QStringList(), 0L, /*canReturnNull=*/true); // The icon does not exists on that computer, import it: if (emblem.isNull()) { // Of the emblem path was eg. "/home/seb/emblem.png", it was exported as "tag-emblems/_home_seb_emblem.png". // So we need to copy that image to "~/.kde/share/apps/basket/tag-emblems/emblem.png": int slashIndex = emblemName.lastIndexOf('/'); QString emblemFileName = (slashIndex < 0 ? emblemName : emblemName.right(slashIndex - 2)); QString source = extractionFolder + "tag-emblems/" + emblemName.replace('/', '_'); QString destination = Global::savesFolder() + "tag-emblems/" + emblemFileName; if (!dir.exists(destination) && dir.exists(source)) copier.copyFolder(source, destination); // Replace the emblem path in the tags.xml copy: QDomElement emblemElement = XMLWork::getElement(subElement, "emblem"); subElement.removeChild(emblemElement); XMLWork::addElement(*document, subElement, "emblem", destination); } } } subNode = subNode.nextSibling(); } } node = node.nextSibling(); } Basket::safelySaveToFile(extractionFolder + "tags.xml", document->toString()); }
void FormatImporter::importBaskets() { kDebug() << "Import Baskets: Preparing..."; // Some preliminary preparations (create the destination folders and the basket tree file): QDir dirPrep; dirPrep.mkdir(Global::savesFolder()); dirPrep.mkdir(Global::basketsFolder()); QDomDocument document("basketTree"); QDomElement root = document.createElement("basketTree"); document.appendChild(root); // First up, establish a list of every baskets, ensure the old order (if any), and count them. QStringList baskets; // Read the 0.5.0 baskets order: QDomDocument *doc = XMLWork::openFile("container", Global::savesFolder() + "container.baskets"); if (doc != 0) { QDomElement docElem = doc->documentElement(); QDomElement basketsElem = XMLWork::getElement(docElem, "baskets"); QDomNode n = basketsElem.firstChild(); while (!n.isNull()) { QDomElement e = n.toElement(); if ((!e.isNull()) && e.tagName() == "basket") baskets.append(e.text()); n = n.nextSibling(); } } // Then load the baskets that weren't loaded (import < 0.5.0 ones): QDir dir(Global::savesFolder(), QString::null, QDir::Name | QDir::IgnoreCase, QDir::Dirs | QDir::NoSymLinks); QStringList list = dir.entryList(); if (list.count() > 2) // Pass "." and ".." for (QStringList::Iterator it = list.begin(); it != list.end(); ++it) // For each folder if (*it != "." && *it != ".." && dir.exists(Global::savesFolder() + *it + "/.basket")) // If it can be a basket folder if (! (baskets.contains((*it) + "/")) && baskets.contains(*it) ) // And if it is not already in the imported baskets list baskets.append(*it); kDebug() << "Import Baskets: Found " << baskets.count() << " baskets to import."; // Import every baskets: int i = 0; for (QStringList::iterator it = baskets.begin(); it != baskets.end(); ++it) { ++i; kDebug() << "Import Baskets: Importing basket " << i << " of " << baskets.count() << "..."; // Move the folder to the new repository (normal basket) or copy the folder (mirorred folder): QString folderName = *it; if (folderName.startsWith("/")) { // It was a folder mirror: KMessageBox::information(0, i18n("<p>Folder mirroring is not possible anymore (see <a href='http://basket.kde.org/'>basket.kde.org</a> for more information).</p>" "<p>The folder <b>%1</b> has been copied for the basket needs. You can either delete this folder or delete the basket, or use both. But remember that " "modifying one will not modify the other anymore as they are now separate entities.</p>", folderName), i18n("Folder Mirror Import"), "", KMessageBox::AllowLink); // Also modify folderName to be only the folder name and not the full path anymore: QString newFolderName = folderName; if (newFolderName.endsWith("/")) newFolderName = newFolderName.left(newFolderName.length() - 1); newFolderName = newFolderName.mid(newFolderName.lastIndexOf('/') + 1); newFolderName = Tools::fileNameForNewFile(newFolderName, Global::basketsFolder()); FormatImporter f; f.copyFolder(folderName, Global::basketsFolder() + newFolderName); folderName = newFolderName; } else dir.rename(Global::savesFolder() + folderName, Global::basketsFolder() + folderName); // Move the folder // Import the basket structure file and get the properties (to add them in the tree basket-properties cache): QDomElement properties = importBasket(folderName); // Add it to the XML document: QDomElement basketElement = document.createElement("basket"); root.appendChild(basketElement); basketElement.setAttribute("folderName", folderName); basketElement.appendChild(properties); } // Finalize (write to disk and delete now useless files): kDebug() << "Import Baskets: Finalizing..."; QFile file(Global::basketsFolder() + "baskets.xml"); if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); stream.setCodec("UTF-8"); QString xml = document.toString(); stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; stream << xml; file.close(); } Tools::deleteRecursively(Global::savesFolder() + ".tmp"); dir.remove(Global::savesFolder() + "container.baskets"); kDebug() << "Import Baskets: Finished."; }