📄 packagewizard.cpp
字号:
/************************************************************************ Copyright (C) 2000-2002 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "packagewizard.h"#include "pkdesc.h"#include <qtopia/process.h>#include <qtopia/global.h>#include <qtopia/resource.h>#include <qtopia/stringutil.h>#include <qtopia/locationcombo.h>#include <qtopia/qpeapplication.h>#ifdef QWS#include <qtopia/qcopenvelope_qws.h>#endif#include <qtopia/applnk.h>#include <qtopia/qprocess.h>#include <qtopia/storage.h>#include <qradiobutton.h>#include <qvbuttongroup.h>#include <qmultilineedit.h>#include <qlayout.h>#include <qprogressbar.h>#include <qcombobox.h>#include <qstylesheet.h>#include <qdict.h>#include <qfile.h>#include <qlineedit.h>#include <qpushbutton.h>#include <qlistview.h>#include <qlistbox.h>#include <qmessagebox.h>#include <qpainter.h>#include <qpixmap.h>#include <qpopupmenu.h>#include <qregexp.h>#include <qtextstream.h>#include <qtextview.h>#include <qtoolbutton.h>#include <qlabel.h>#include <stdlib.h>#include <unistd.h> //symlink() #include <sys/stat.h> // mkdir()static QPixmap *pm_uninstalled=0;static QPixmap *pm_installed=0;static QPixmap *pm_uninstall=0;static QPixmap *pm_install=0;static QString packageId(const QString& name_or_file){ // clean up pkg. If we have a whole file, want just the name of the package. // If we have a _VER_ARCH.ipk ending, try to slim down the name to what // the package name probably is. int start = name_or_file.find(QRegExp("[^/_]+_[^/]*_[^/_]*.ipk$")); int end = name_or_file.find(QRegExp("_[^/]*_[^/_]*.ipk$")); if (start > 0 && end > start) return name_or_file.mid(start, end - start); else return name_or_file;}/*class DetailsPopup : public QPopupMenu { QLabel* label;public: DetailsPopup(QWidget* parent) : QPopupMenu(parent) { label = new QLabel(this); label->setTextFormat(RichText); insertItem(label); } void setText(const QString& s) { label->setText(s); }};*/class ServerListItem : public QCheckListItem { bool isdup;public: ServerListItem(QListView *parent, const QString &text) : QCheckListItem(parent,text,CheckBox), isdup(FALSE) { } void setDup(bool y) { if ( isdup != y ) { isdup = y; repaint(); } } void paintCell( QPainter *p, const QColorGroup & cg, int column, int width, int alignment ) { QColorGroup c = cg; if ( column==0 && isdup ) c.setColor(QColorGroup::Text,red); QCheckListItem::paintCell(p,c,column,width,alignment); } void setName(const QString& n) { setText(0,n); } QString name() const { return text(0); } static QString id(const QString& nm) { QString n = nm; for (int i=0; i<(int)n.length(); ++i) if ( n[i]==' ' ) n[i] = '_'; return n; } QString id() const { return id(name()); } void setOn(bool y) { // Force listview's signals listView()->setSelected(this,FALSE); QCheckListItem::setOn(y); listView()->setSelected(this,y); }};static bool parseInfo(const QStringList& lines, QString& name, QString& description_short, QString& description, QString& size, QString& sec){ QRegExp separatorRegExp( ":[\t ]+" ); bool in_desc=FALSE; for (QStringList::ConstIterator it = lines.begin(); it!=lines.end(); ++it) { QString line = *it; if ( line[0] == ' ' || line[0] == '\t' ) { // continuation if ( in_desc ) description+=line; } else { int sep = line.find(separatorRegExp); if ( sep >= 0 ) { QString tag = line.left(sep); in_desc=FALSE; if ( tag == "Package" ) { // No tr name = line.mid(sep+2).simplifyWhiteSpace(); } else if ( tag == "Description" ) { // No tr description_short = line.mid(sep+2).simplifyWhiteSpace(); in_desc = TRUE; } else if ( tag == "Installed-Size" ) { size = line.mid(sep+2).simplifyWhiteSpace(); } else if ( tag == "Section" ) { // No tr sec = line.mid(sep+2).simplifyWhiteSpace(); } description += line + "<p>"; } } } if ( !name.isEmpty() ) { description_short[0] = description_short[0].upper(); if ( description_short.left(4) == "The " ) // No tr description_short = description_short.mid(4); if ( description_short.left(2) == "A " ) // No tr description_short = description_short.mid(2); description_short[0] = description_short[0].upper(); return TRUE; } return FALSE;}class PackageItem : public QListViewItem { bool installed; DocLnk link; QString full;public: PackageItem(QListView* lv, const QString& name, const QString& desc, const QString& fulld, const QString& size, bool inst ) : QListViewItem(lv,name), installed(inst), full(fulld) { setText(1,desc); setText(2,size); } PackageItem(QListView* lv, const DocLnk& lnk, bool inst ) : QListViewItem(lv,packageId(lnk.file())), installed(inst), link(lnk) { } QString id() const { if ( link.fileKnown() ) { return file(); } else { return text(0); } } QString file() const { return link.file(); } void setInfo(const QString& c) { parseInfo(c); } bool parseInfo() { return parseInfo(link.comment()); } bool parseInfo(const QString& comment) { QStringList info = QStringList::split("\n",comment); QString name; QString description_short; QString description; QString size; QString sec; if ( ::parseInfo(info, name, description_short, description, size, sec ) ) { setName(name); setDescription(description_short); setFullDescription(description); setSize(size); return TRUE; } else { setDescription("?"); setFullDescription("?"); setSize("?"); return FALSE; } } void setName(const QString& txt) { setText(0, txt); } void setDescription(const QString& txt) { setText(1, txt); } QString fullDescription() const { return full; } void setFullDescription(const QString& txt) { full = txt; } void setSize(const QString& txt) { setText(2, txt); } void paintCell( QPainter *p, const QColorGroup & cg, int column, int width, int alignment ) { if ( !p ) return; p->fillRect( 0, 0, width, height(), isSelected()? cg.highlight() : cg.base() ); if ( column != 0 ) { // The rest is text QListViewItem::paintCell( p, cg, column, width, alignment ); return; } QListView *lv = listView(); if ( !lv ) return; int marg = lv->itemMargin(); int r = marg; QPixmap pm = statePixmap(); p->drawPixmap(marg,(height()-pm.height())/2,pm); r += pm.width()+1; p->translate( r, 0 ); QListViewItem::paintCell( p, cg, column, width - r, alignment ); } QPixmap statePixmap() const { if ( !isSelected() ) { if ( !installed ) return *pm_uninstalled; else return *pm_installed; } else { if ( !installed ) return *pm_install; else return *pm_uninstall; } } QString name() const { return text(0); } QString description() const { return text(1); } bool isInstalled() const { return installed; } QString key( int column, bool ascending ) const { if ( column == 2 ) { QString t = text(2); double bytes=t.toDouble(); if ( t.contains('M') ) bytes*=1024*1024; else if ( t.contains('K') || t.contains('k') ) bytes*=1024; if ( !ascending ) bytes=999999999-bytes; return QString().sprintf("%09d",(int)bytes); } else { return QListViewItem::key(column,ascending); } }};/* * Constructs a PackageWizard which is a child of 'parent', with the * name 'name' and widget flags set to 'f' */PackageWizard::PackageWizard( QWidget* parent, const char* name, WFlags fl ) : PackageWizardBase( parent, name, fl ){ infoProcess = 0; committed = FALSE; installedRootDict = 0; connect( newserver, SIGNAL(clicked()), this, SLOT(newServer()) ); connect( removeserver, SIGNAL(clicked()), this, SLOT(removeServer()) ); connect( servers, SIGNAL(currentChanged(QListViewItem*)), this, SLOT(editServer(QListViewItem*)) ); servername->setEnabled(FALSE); serverurl->setEnabled(FALSE); if (!pm_uninstalled) { pm_uninstalled = new QPixmap(Resource::loadPixmap("uninstalled")); pm_installed = new QPixmap(Resource::loadPixmap("installed")); pm_install = new QPixmap(Resource::loadPixmap("install")); pm_uninstall = new QPixmap(Resource::loadPixmap("uninstall")); } packagelist->setColumnWidthMode(0,QListView::Manual); packagelist->setColumnWidthMode(1,QListView::Manual); packagelist->setColumnWidthMode(2,QListView::Manual); packagelist->setColumnAlignment(2,AlignRight); packagelist->setSelectionMode( QListView::Multi ); packagelist->setVScrollBarMode(QScrollView::AlwaysOn); packagelist->installEventFilter(this); packagelist->setShowSortIndicator(TRUE); ipkg_old = 0; readSettings(); updateServerSelection(); progress_net->hide(); progress_confirm->hide(); QPEApplication::setStylusOperation(packagelist->viewport(),QPEApplication::RightOnHold); //package_description = new DetailsPopup(this); delete mode_share; // ### Not implemented yet setHelpEnabled(page_mode,FALSE); setHelpEnabled(page_servers,FALSE); setHelpEnabled(page_packages,FALSE); setHelpEnabled(page_share,FALSE); setHelpEnabled(page_location,FALSE); setHelpEnabled(page_confirmation,FALSE);}/* * Destroys the object and frees any allocated resources */PackageWizard::~PackageWizard(){ delete installedRootDict; // no need to delete child widgets, Qt does it all for us}QProgressBar* PackageWizard::progress(){ if ( currentPage() == page_confirmation ) return progress_confirm; else return progress_net;}void PackageWizard::setAppropriates(){ setNextEnabled( page_mode, !!mode_choice->selected() ); setNextEnabled( page_share, !!share_choice->selected() ); updatePackageNext(); setFinishEnabled( page_confirmation, TRUE ); setAppropriate( page_mode, qcopDocument.isEmpty() ); setAppropriate( page_servers, !committed && mode_net->isChecked() ); setAppropriate( page_packages, !committed && qcopDocument.isEmpty() ); bool need_loc = !mode_rm->isChecked(); /* if ( mode_share->isChecked() ) { // If sharing to a Document, need location // ... } */ setAppropriate( page_location, !committed && need_loc ); setAppropriate( page_share, FALSE ); // ( !committed && mode_share->isChecked() );}void PackageWizard::updatePackageNext(){ bool any = FALSE; for (QListViewItem* i = packagelist->firstChild(); !any && i; i = i->nextSibling()) { PackageItem* item = (PackageItem*)i; if ( item->isSelected() ) any = TRUE; } setNextEnabled( page_packages, any ); PackageItem* cur = (PackageItem*)packagelist->currentItem(); details->setEnabled(!!cur);}void PackageWizard::showDetails(){ PackageItem* cur = (PackageItem*)packagelist->currentItem(); if ( cur ) { QString d = cur->fullDescription(); if ( d.isEmpty() ) d = tr("No details."); PackageDetails det(this,0,TRUE); det.setCaption(tr("Package: %1").arg(cur->name())); det.description->setText(d); det.showMaximized(); cur->setSelected(det.exec()); updatePackageNext(); }}void PackageWizard::updateServerSelection(){ int nservers = 0; QDict<void> dict; bool dups=FALSE; for (QListViewItem* i = servers->firstChild(); i; i = i->nextSibling()) { ServerListItem* item = (ServerListItem*)i; if ( dict.find(item->id()) ) { dups = TRUE; item->setDup(TRUE); } else { item->setDup(FALSE); } dict.insert(item->id(),(void*)1); if ( item->isOn() ) nservers++; } setNextEnabled( page_servers, nservers>0 && !dups );}void PackageWizard::showPage( QWidget* w ){ if ( w != currentPage() ) { committed = FALSE; if ( w == page_servers ) { } else if ( w == page_packages ) { if ( currentPage() == page_mode || currentPage() == page_servers ) { // Slow, show page now packagelist->clear(); PackageWizardBase::showPage(w); qApp->processEvents(); updatePackageList(); } } else if ( w == page_share ) { } else if ( w == page_location ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -