Author: kmachalkova Date: Thu Feb 28 12:10:38 2008 New Revision: 45099
URL: http://svn.opensuse.org/viewcvs/yast?rev=45099&view=rev Log: export/import files moved here
Modified: branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.cc branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.h
Modified: branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.cc URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/bubli/ncurses-pkg/src/NCPk... ============================================================================== --- branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.cc (original) +++ branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.cc Thu Feb 28 12:10:38 2008 @@ -18,6 +18,15 @@ #include "Y2Log.h" #include "NCPkgMenuExtras.h"
+#include <boost/bind.hpp> +#include <fstream> +#include <iomanip> +#include <zypp/SysContent.h> + +typedef zypp::syscontent::Reader::Entry ZyppReaderEntry; +typedef std::pair<string, ZyppReaderEntry> importMapPair; + +#define DEFAULT_EXPORT_FILE_NAME "user-packages.xml"
/* Textdomain "packages" @@ -54,13 +63,228 @@ if (!event.selection) return false;
- if ( event.selection == diskSpace ) + if ( event.selection == exportFile ) + exportToFile(); + else if ( event.selection == importFile ) + importFromFile(); + else if ( event.selection == diskSpace ) showDiskSpace(); else NCINT << "zatim nic" << endl; return true; }
+void NCPkgMenuExtras::importSelectable( ZyppSel selectable, bool isWanted, const char*kind ) +{ + ZyppStatus oldStatus = selectable->status(); + ZyppStatus newStatus = oldStatus; + + //Package/Pattern is on the list + if (isWanted) + { + switch (oldStatus) + { + //Keep status for installed ones + case S_Install: + case S_AutoInstall: + case S_Update: + case S_AutoUpdate: + case S_KeepInstalled: + case S_Protected: + newStatus = oldStatus; + break; + + //Keep also those marked for deletion + case S_Del: + case S_AutoDel: + newStatus = S_KeepInstalled; + NCDBG << "Keeping " << kind << " " << selectable->name().c_str() << endl; + break; + + //Add not yet installed pkgs (if they have candidate available) + case S_NoInst: + case S_Taboo: + if ( selectable->hasCandidateObj() ) + { + newStatus = S_Install; + NCDBG << "Adding " << kind << " " << selectable->name().c_str() << endl; + } + else + { + NCDBG << "Cannot add " << kind << " " << selectable->name().c_str() << + " " << " - no candidate." << endl; + } + break; + } + } + //Package/Pattern is not on the list + else + { + switch (oldStatus) + { + //Mark installed ones for deletion + case S_Install: + case S_AutoInstall: + case S_Update: + case S_AutoUpdate: + case S_KeepInstalled: + case S_Protected: + newStatus = S_Del; + NCDBG << "Deleting " << kind << " " << selectable->name().c_str() << endl; + break; + + //Keep status for not installed, taboo and to-be-deleted + case S_Del: + case S_AutoDel: + case S_NoInst: + case S_Taboo: + newStatus = oldStatus; + break; + } + } + + if (oldStatus != newStatus) + selectable->set_status( newStatus ); +} + + +bool NCPkgMenuExtras::exportToFile() +{ //Ask for file to save into + string filename = YUI::app()->askForSaveFileName( DEFAULT_EXPORT_FILE_NAME, + "*.xml", + _("Export List of All Packages and Patterns to File" )); + + if ( ! filename.empty() ) + { + zypp::syscontent::Writer writer; + const zypp::ResPool & pool = zypp::getZYpp()->pool(); + + //some strange C++ magic (c) by ma + for_each( pool.begin(), pool.end(), + boost::bind( &zypp::syscontent::Writer::addIf, + boost::ref(writer), + _1)); + + try + { + //open file for writing and try to dump syscontent into it + std::ofstream exportFile( filename.c_str() ); + exportFile.exceptions(std::ios_base::badbit | std::ios_base::failbit ); + exportFile << writer; + + NCMIL << "Exported list of packages and patterns to " << filename << endl; + } + + catch (std::exception & exception) + { + NCWAR << "Error exporting list of packages and patterns to " << filename << endl; + + //delete partially written file (don't care if it doesn't exist) + (void) unlink( filename.c_str() ); + + //present error popup to the user + NCPopupInfo * errorMsg = new NCPopupInfo( wpos( (NCurses::lines()-5)/2, (NCurses::cols()-40)/2 ), + NCPkgNames::ErrorLabel(), + _("Error exporting list of packages and patterns to ") + // FIXME: String addition is evil for translators! + + filename, + NCPkgNames::OKLabel(), + ""); + errorMsg->setNiceSize(40,5); + NCursesEvent input = errorMsg->showInfoPopup(); + + YDialog::deleteTopmostDialog(); + } + + return true; + } + +} + +bool NCPkgMenuExtras::importFromFile() +{ + //ask for file to open + string filename = YUI::app()->askForExistingFile( DEFAULT_EXPORT_FILE_NAME, + "*.xml", + _("Import List of All Packages and Patterns from File")); + if ( ! filename.empty() ) + { + NCPkgTable * packageList = pkg->PackageList(); + NCMIL << "Importing list of packages and patterns from " << filename << endl; + + try + { + std::ifstream importFile ( filename.c_str() ); + zypp::syscontent::Reader reader (importFile); + + //maps to store package/pattern data into + map<string, ZyppReaderEntry> importPkgs; + map<string, ZyppReaderEntry> importPatterns; + + //Import syscontent reader to a map $[ "package_name" : pointer_to_data] + for (zypp::syscontent::Reader::const_iterator it = reader.begin(); + it != reader.end(); + it ++ ) + { + string kind = it->kind(); + + // importMapPair => std::pair<string, ZyppReaderEntry> + if ( kind == "package" ) + importPkgs.insert( importMapPair( it->name(), *it ) ); + else if ( kind == "pattern" ) + importPatterns.insert( importMapPair( it->name(), *it ) ); + } + + NCMIL << "Found " << importPkgs.size() << " packages and " << importPatterns.size() << " patterns." << endl; + + //Change status of appropriate packages and patterns + for (ZyppPoolIterator it = zyppPkgBegin(); + it != zyppPkgEnd(); + it++ ) + { + ZyppSel selectable = *it; + //isWanted => package name found in importPkgs map + importSelectable ( *it, importPkgs.find( selectable->name() ) != importPkgs.end(), "package" ); + } + + for (ZyppPoolIterator it = zyppPatternsBegin(); + it != zyppPatternsEnd(); + it++ ) + { + ZyppSel selectable = *it; + importSelectable ( *it, importPatterns.find( selectable->name() ) != importPatterns.end(), "pattern" ); + } + + //Switch to installation summary filter + pkg->fillSummaryList(NCPkgTable::L_Changes); + + //... and finally display the result + packageList->showInformation(); + packageList->setKeyboardFocus(); + + return true; + } + catch ( const zypp::Exception & exception ) + { + NCWAR << "Error importing list of packages and patterns from" << filename << endl; + + NCPopupInfo * errorMsg = new NCPopupInfo( wpos( (NCurses::lines()-5)/2, (NCurses::cols()-40)/2) , + NCPkgNames::ErrorLabel(), + _("Error importing list of packages and patterns from ") + // FIXME: String addition is evil for translators! + + filename, + NCPkgNames::OKLabel(), + ""); + errorMsg->setNiceSize(40,5); + NCursesEvent input = errorMsg->showInfoPopup(); + + YDialog::deleteTopmostDialog(); + } + } + return true; + +} + bool NCPkgMenuExtras::showDiskSpace() { pkg->diskSpacePopup()->showInfoPopup( NCPkgNames::DiskspaceLabel() );
Modified: branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.h URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/bubli/ncurses-pkg/src/NCPk... ============================================================================== --- branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.h (original) +++ branches/tmp/bubli/ncurses-pkg/src/NCPkgMenuExtras.h Thu Feb 28 12:10:38 2008 @@ -49,6 +49,12 @@
bool handleEvent (const NCursesEvent & event);
+ void importSelectable ( ZyppSel selectable, bool isWanted, const char*kind ); + + bool exportToFile(); + + bool importFromFile(); + bool showDiskSpace();
};