📄 packagewizard.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program 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 General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** 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;//#define IPKGSIM#ifdef IPKGSIMstatic const char *ipkg_name = "ipkgsim";#elsestatic const char *ipkg_name = "ipkg";#endifstatic QString packageId(const QString& name_or_file){ // clean up pkg. If we have a whole file, we 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); } }};// ====================================================================class LabelMaker{public: LabelMaker(QLabel *lbl) : mLbl( lbl ), message(), extraPackages( 0 ), tooManyPackages( FALSE ) { QWidget *gparent = mLbl->parentWidget()->parentWidget(); int gpheight = gparent->geometry().height(); int fheight = QFontMetrics(QFont()).height(); maxheight = gpheight - (3 * fheight); } void addString(QString s) { message += s; mLbl->setText( message ); } void addPackage(QString s) { if ( tooManyPackages ) { oneMorePackage(); return; } QString adding = QString("<li>%1").arg(s); uint curSize = message.length(); addString(adding); mLbl->adjustSize(); if ( mLbl->geometry().height() >= maxheight ) { // oops, we're too long, take off that last line moreIndex = curSize; oneMorePackage(); tooManyPackages = TRUE; } }private: void oneMorePackage() { extraPackages++; message.truncate( moreIndex ); addString( QString("<li>%1 more packages").arg( extraPackages ) ); } QLabel *mLbl; QString message; int maxheight; uint extraPackages; uint moreIndex; bool tooManyPackages;};// ====================================================================/* * Constructs a PackageWizard that 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 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -