41 #define YUILogComponent "ncurses-pkg"
44 #include "NCPkgMenuExtras.h"
45 #include "NCPackageSelector.h"
46 #include "NCPopupInfo.h"
48 #include <boost/bind.hpp>
51 #include <zypp/SysContent.h>
55 typedef zypp::syscontent::Reader::Entry ZyppReaderEntry;
56 typedef std::pair<std::string, ZyppReaderEntry> importMapPair;
58 #define DEFAULT_EXPORT_FILE_NAME "user-packages.xml"
64 NCPkgMenuExtras::NCPkgMenuExtras (YWidget *parent, std::string label,
NCPackageSelector *pkger)
65 : NCMenuButton( parent, label)
71 NCPkgMenuExtras::~NCPkgMenuExtras()
76 void NCPkgMenuExtras::createLayout()
78 exportFile =
new YMenuItem( _(
"&Export Package List to File" ) );
79 items.push_back( exportFile );
81 importFile =
new YMenuItem( _(
"&Import Package List from File" ) );
82 items.push_back( importFile );
84 diskSpace =
new YMenuItem( _(
"&Show Available Disk Space" ) );
85 items.push_back( diskSpace );
90 bool NCPkgMenuExtras::handleEvent (
const NCursesEvent & event)
95 if ( event.selection == exportFile )
97 else if ( event.selection == importFile )
99 else if ( event.selection == diskSpace )
113 void NCPkgMenuExtras::importSelectable( ZyppSel selectable,
bool isWanted,
const char*kind )
115 ZyppStatus oldStatus = selectable->status();
116 ZyppStatus newStatus = oldStatus;
128 case S_KeepInstalled:
130 newStatus = oldStatus;
136 newStatus = S_KeepInstalled;
137 yuiDebug() <<
"Keeping " << kind <<
" " << selectable->name().c_str() << endl;
143 if ( selectable->hasCandidateObj() )
145 newStatus = S_Install;
146 yuiDebug() <<
"Adding " << kind <<
" " << selectable->name().c_str() << endl;
150 yuiDebug() <<
"Cannot add " << kind <<
" " << selectable->name().c_str() <<
151 " " <<
" - no candidate." << endl;
166 case S_KeepInstalled:
169 yuiDebug() <<
"Deleting " << kind <<
" " << selectable->name().c_str() << endl;
177 newStatus = oldStatus;
182 if (oldStatus != newStatus)
183 selectable->setStatus( newStatus );
187 bool NCPkgMenuExtras::exportToFile()
189 std::string filename = YUI::app()->askForSaveFileName( DEFAULT_EXPORT_FILE_NAME,
191 _(
"Export List of All Packages and Patterns to File" ));
193 if ( ! filename.empty() )
195 zypp::syscontent::Writer writer;
196 const zypp::ResPool & pool = zypp::getZYpp()->pool();
199 for_each( pool.begin(), pool.end(),
200 boost::bind( &zypp::syscontent::Writer::addIf,
207 std::ofstream exportFile( filename.c_str() );
208 exportFile.exceptions(std::ios_base::badbit | std::ios_base::failbit );
209 exportFile << writer;
211 yuiMilestone() <<
"Exported list of packages and patterns to " << filename << endl;
214 catch (std::exception & exception)
216 yuiWarning() <<
"Error exporting list of packages and patterns to " << filename << endl;
219 (void) unlink( filename.c_str() );
222 NCPopupInfo * errorMsg =
new NCPopupInfo( wpos( (NCurses::lines()-5)/2, (NCurses::cols()-40)/2 ),
223 NCPkgStrings::ErrorLabel(),
224 _(
"Error exporting list of packages and patterns to " )
229 errorMsg->setPreferredSize(40,5);
230 NCursesEvent input = errorMsg->showInfoPopup();
232 YDialog::deleteTopmostDialog();
241 bool NCPkgMenuExtras::importFromFile()
244 std::string filename = YUI::app()->askForExistingFile( DEFAULT_EXPORT_FILE_NAME,
246 _(
"Import List of All Packages and Patterns from File" ));
247 if ( ! filename.empty() )
249 NCPkgTable * packageList = pkg->PackageList();
250 yuiMilestone() <<
"Importing list of packages and patterns from " << filename << endl;
254 std::ifstream importFile ( filename.c_str() );
255 zypp::syscontent::Reader reader (importFile);
258 std::map<std::string, ZyppReaderEntry> importPkgs;
259 std::map<std::string, ZyppReaderEntry> importPatterns;
262 for (zypp::syscontent::Reader::const_iterator it = reader.begin();
266 std::string kind = it->kind();
269 if ( kind ==
"package" )
270 importPkgs.insert( importMapPair( it->name(), *it ) );
271 else if ( kind ==
"pattern" )
272 importPatterns.insert( importMapPair( it->name(), *it ) );
275 yuiMilestone() <<
"Found " << importPkgs.size() <<
" packages and " << importPatterns.size() <<
" patterns." << endl;
278 for (ZyppPoolIterator it = zyppPkgBegin();
282 ZyppSel selectable = *it;
284 importSelectable ( *it, importPkgs.find( selectable->name() ) != importPkgs.end(),
"package" );
287 for (ZyppPoolIterator it = zyppPatternsBegin();
288 it != zyppPatternsEnd();
291 ZyppSel selectable = *it;
292 importSelectable ( *it, importPatterns.find( selectable->name() ) != importPatterns.end(),
"pattern" );
296 packageList->fillSummaryList(NCPkgTable::L_Changes);
300 packageList->setKeyboardFocus();
304 catch (
const zypp::Exception & exception )
306 yuiWarning() <<
"Error importing list of packages and patterns from" << filename << endl;
308 NCPopupInfo * errorMsg =
new NCPopupInfo( wpos( (NCurses::lines()-5)/2, (NCurses::cols()-40)/2) ,
309 NCPkgStrings::ErrorLabel(),
310 _(
"Error importing list of packages and patterns from " )
315 errorMsg->setPreferredSize(40,5);
316 NCursesEvent input = errorMsg->showInfoPopup();
318 YDialog::deleteTopmostDialog();
325 bool NCPkgMenuExtras::showDiskSpace()
bool showInformation()
Show the corresponding information (e.g.
static const std::string DiskspaceLabel()
The headline of the disk space popup.
static const std::string OKLabel()
The label of the OK button.