kmmainview.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kmmainview.h"
00021 #include "kmtimer.h"
00022 #include "kmprinterview.h"
00023 #include "kmpages.h"
00024 #include "kmmanager.h"
00025 #include "kmuimanager.h"
00026 #include "kmfactory.h"
00027 #include "kmvirtualmanager.h"
00028 #include "kmprinter.h"
00029 #include "driver.h"
00030 #include "kmdriverdialog.h"
00031 #include "kmwizard.h"
00032 #include "kmconfigdialog.h"
00033 #include "kmspecialprinterdlg.h"
00034 #include "plugincombobox.h"
00035 #include "kiconselectaction.h"
00036 #include "messagewindow.h"
00037 
00038 #include <qdockarea.h>
00039 #include <kmenubar.h>
00040 #include <qtimer.h>
00041 #include <qcombobox.h>
00042 #include <qlabel.h>
00043 #include <qlayout.h>
00044 #include <qpopupmenu.h>
00045 #include <kmessagebox.h>
00046 #include <kaction.h>
00047 #include <klocale.h>
00048 #include <kconfig.h>
00049 #include <ktoolbar.h>
00050 #include <ktoolbarbutton.h>
00051 #include <kdebug.h>
00052 #include <kpopupmenu.h>
00053 #include <klibloader.h>
00054 #include <kdialogbase.h>
00055 #include <ksimpleconfig.h>
00056 #include <kstandarddirs.h>
00057 #include <kapplication.h>
00058 #include <qprocess.h>
00059 
00060 #undef m_manager
00061 #define m_manager   KMFactory::self()->manager()
00062 
00063 int kdeprint_management_add_printer_wizard( QWidget* parent )
00064 {
00065         KMWizard    dlg(parent);
00066         int     flag(0);
00067         if (dlg.exec())
00068         {
00069             flag = 1;
00070             // check if the printer already exists, and ask confirmation if needed.
00071             if (KMFactory::self()->manager()->findPrinter(dlg.printer()->name()) != 0)
00072                 if (KMessageBox::warningContinueCancel(parent,i18n("The printer %1 already exists. Continuing will overwrite existing printer. Do you want to continue?").arg(dlg.printer()->name())) == KMessageBox::Cancel)
00073                     flag = 0;
00074             // try to add printer only if flag is true.
00075             if (flag && !KMFactory::self()->manager()->createPrinter(dlg.printer()))
00076                 flag = -1;
00077         }
00078         return flag;
00079 }
00080 
00081 KMMainView::KMMainView(QWidget *parent, const char *name, KActionCollection *coll)
00082 : QWidget(parent, name)
00083 {
00084     m_current = 0;
00085     m_first = true;
00086 
00087     // create widgets
00088     m_printerview = new KMPrinterView(this, "PrinterView");
00089     m_printerpages = new KMPages(this, "PrinterPages");
00090     m_pop = new QPopupMenu(this);
00091     m_toolbar = new KToolBar(this, "ToolBar");
00092     m_toolbar->setMovingEnabled(false);
00093     m_plugin = new PluginComboBox(this, "Plugin");
00094     /*
00095     m_menubar = new KMenuBar( this );
00096     static_cast<KMenuBar*>( m_menubar )->setTopLevelMenu( false );
00097     */
00098     m_menubar = new KToolBar( this, "MenuBar", false, false );
00099     m_menubar->setIconText( KToolBar::IconTextRight );
00100     m_menubar->setMovingEnabled( false );
00101 
00102     // layout
00103     QVBoxLayout *m_layout = new QVBoxLayout(this, 0, 0);
00104     m_layout->addWidget(m_toolbar);
00105     m_layout->addWidget( m_menubar );
00106     m_boxlayout = new QBoxLayout(QBoxLayout::TopToBottom, 0, 0);
00107     m_layout->addLayout(m_boxlayout);
00108     m_boxlayout->addWidget(m_printerview);
00109     m_boxlayout->addWidget(m_printerpages);
00110     m_layout->addSpacing(5);
00111     m_layout->addWidget(m_plugin, 0);
00112 
00113     // connections
00114     connect(KMTimer::self(),SIGNAL(timeout()),SLOT(slotTimer()));
00115     connect(m_printerview,SIGNAL(printerSelected(const QString&)),SLOT(slotPrinterSelected(const QString&)));
00116     connect(m_printerview,SIGNAL(rightButtonClicked(const QString&,const QPoint&)),SLOT(slotRightButtonClicked(const QString&,const QPoint&)));
00117     connect(m_pop,SIGNAL(aboutToShow()),KMTimer::self(),SLOT(hold()));
00118     connect(m_pop,SIGNAL(aboutToHide()),KMTimer::self(),SLOT(release()));
00119     connect( m_manager, SIGNAL( updatePossible( bool ) ), SLOT( slotUpdatePossible( bool ) ) );
00120 
00121     // actions
00122     if (coll)
00123         m_actions = coll;
00124     else
00125         m_actions = new KActionCollection(this);
00126     initActions();
00127 
00128     // first update
00129     restoreSettings();
00130     loadParameters();
00131 
00132     // delay first update until KMManager is ready
00133     reset( i18n( "Initializing manager..." ), true, true );
00134 }
00135 
00136 KMMainView::~KMMainView()
00137 {
00138     saveSettings();
00139     //KMFactory::release();
00140 }
00141 
00142 void KMMainView::loadParameters()
00143 {
00144 }
00145 
00146 void KMMainView::restoreSettings()
00147 {
00148     KConfig *conf = KMFactory::self()->printConfig();
00149     conf->setGroup("General");
00150     setViewType((KMPrinterView::ViewType)conf->readNumEntry("ViewType",KMPrinterView::Icons));
00151     setOrientation(conf->readNumEntry("Orientation", Qt::Vertical));
00152     bool    view = conf->readBoolEntry("ViewToolBar",false);
00153     slotToggleToolBar(view);
00154     ((KToggleAction*)m_actions->action("view_toolbar"))->setChecked(view);
00155     view = conf->readBoolEntry( "ViewMenuBar", true );
00156     slotToggleMenuBar( view );
00157     static_cast<KToggleAction*>( m_actions->action( "view_menubar" ) )->setChecked( view );
00158     view = conf->readBoolEntry("ViewPrinterInfos",true);
00159     slotShowPrinterInfos(view);
00160     ((KToggleAction*)m_actions->action("view_printerinfos"))->setChecked(view);
00161 }
00162 
00163 void KMMainView::saveSettings()
00164 {
00165     KConfig *conf = KMFactory::self()->printConfig();
00166     conf->setGroup("General");
00167     conf->writeEntry("ViewType",(int)m_printerview->viewType());
00168     conf->writeEntry("Orientation",(int)orientation());
00169     conf->writeEntry("ViewToolBar",((KToggleAction*)m_actions->action("view_toolbar"))->isChecked());
00170     conf->writeEntry("ViewMenuBar",static_cast<KToggleAction*>( m_actions->action("view_menubar") )->isChecked());
00171     conf->writeEntry("ViewPrinterInfos",((KToggleAction*)m_actions->action("view_printerinfos"))->isChecked());
00172     conf->sync();
00173 }
00174 
00175 void KMMainView::initActions()
00176 {
00177     KIconSelectAction   *vact = new KIconSelectAction(i18n("&View"),0,m_actions,"view_change");
00178     QStringList iconlst;
00179     iconlst << "view_icon" << "view_detailed" << "view_tree";
00180     vact->setItems(QStringList::split(',',i18n("&Icons,&List,&Tree"),false), iconlst);
00181     vact->setCurrentItem(0);
00182     connect(vact,SIGNAL(activated(int)),SLOT(slotChangeView(int)));
00183 
00184     KActionMenu *stateAct = new KActionMenu(i18n("Start/Stop Printer"), "kdeprint_printstate", m_actions, "printer_state_change");
00185     stateAct->setDelayed(false);
00186     stateAct->insert(new KAction(i18n("&Start Printer"),"kdeprint_enableprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_start"));
00187     stateAct->insert(new KAction(i18n("Sto&p Printer"),"kdeprint_stopprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_stop"));
00188 
00189     stateAct = new KActionMenu(i18n("Enable/Disable Job Spooling"), "kdeprint_queuestate", m_actions, "printer_spool_change");
00190     stateAct->setDelayed(false);
00191     stateAct->insert(new KAction(i18n("&Enable Job Spooling"),"kdeprint_enableprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_enable"));
00192     stateAct->insert(new KAction(i18n("&Disable Job Spooling"),"kdeprint_stopprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_disable"));
00193 
00194     new KAction(i18n("&Remove"),"edittrash",0,this,SLOT(slotRemove()),m_actions,"printer_remove");
00195     new KAction(i18n("&Configure..."),"configure",0,this,SLOT(slotConfigure()),m_actions,"printer_configure");
00196     new KAction(i18n("Add &Printer/Class..."),"kdeprint_addprinter",0,this,SLOT(slotAdd()),m_actions,"printer_add");
00197     new KAction(i18n("Add &Special (pseudo) Printer..."),"kdeprint_addpseudo",0,this,SLOT(slotAddSpecial()),m_actions,"printer_add_special");
00198     new KAction(i18n("Set as &Local Default"),"kdeprint_defaulthard",0,this,SLOT(slotHardDefault()),m_actions,"printer_hard_default");
00199     new KAction(i18n("Set as &User Default"),"kdeprint_defaultsoft",0,this,SLOT(slotSoftDefault()),m_actions,"printer_soft_default");
00200     new KAction(i18n("&Test Printer..."),"kdeprint_testprinter",0,this,SLOT(slotTest()),m_actions,"printer_test");
00201     new KAction(i18n("Configure &Manager..."),"kdeprint_configmgr",0,this,SLOT(slotManagerConfigure()),m_actions,"manager_configure");
00202     new KAction(i18n("Initialize Manager/&View"),"reload",0,this,SLOT(slotInit()),m_actions,"view_refresh");
00203 
00204     KIconSelectAction   *dact = new KIconSelectAction(i18n("&Orientation"),0,m_actions,"orientation_change");
00205     iconlst.clear();
00206     iconlst << "view_top_bottom" << "view_left_right";
00207     dact->setItems(QStringList::split(',',i18n("&Vertical,&Horizontal"),false), iconlst);
00208     dact->setCurrentItem(0);
00209     connect(dact,SIGNAL(activated(int)),SLOT(slotChangeDirection(int)));
00210 
00211     new KAction(i18n("R&estart Server"),"kdeprint_restartsrv",0,this,SLOT(slotServerRestart()),m_actions,"server_restart");
00212     new KAction(i18n("Configure &Server..."),"kdeprint_configsrv",0,this,SLOT(slotServerConfigure()),m_actions,"server_configure");
00213     enableBrowsing = new KToggleAction(i18n("Access Printers on Local Network"),0,this,SLOT(slotEnableBrowsing()),m_actions,"enable_browsing");
00214     enableSharing = new KToggleAction(i18n("Share Printers on Local Network"),0,this,SLOT(slotEnableSharing()),m_actions,"enable_sharing");
00215 
00216     setBrowsingStatus();
00217     setSharingStatus();
00218 
00219     KToggleAction   *tact = new KToggleAction(i18n("Show &Toolbar"),0,m_actions,"view_toolbar");
00220     tact->setCheckedState(i18n("Hide &Toolbar"));
00221     connect(tact,SIGNAL(toggled(bool)),SLOT(slotToggleToolBar(bool)));
00222     tact = new KToggleAction( i18n( "Show Me&nu Toolbar" ), 0, m_actions, "view_menubar" );
00223     tact->setCheckedState(i18n("Hide Me&nu Toolbar"));
00224     connect( tact, SIGNAL( toggled( bool ) ), SLOT( slotToggleMenuBar( bool ) ) );
00225     tact = new KToggleAction(i18n("Show Pr&inter Details"),"kdeprint_printer_infos", 0,m_actions,"view_printerinfos");
00226     tact->setCheckedState(KGuiItem(i18n("Hide Pr&inter Details"),"kdeprint_printer_infos"));
00227     tact->setChecked(true);
00228     connect(tact,SIGNAL(toggled(bool)),SLOT(slotShowPrinterInfos(bool)));
00229 
00230     tact = new KToggleAction(i18n("Toggle Printer &Filtering"), "filter", 0, m_actions, "view_pfilter");
00231     tact->setChecked(KMManager::self()->isFilterEnabled());
00232     connect(tact, SIGNAL(toggled(bool)), SLOT(slotToggleFilter(bool)));
00233 
00234     new KAction( i18n( "%1 &Handbook" ).arg( "KDEPrint" ), "contents", 0, this, SLOT( slotHelp() ), m_actions, "invoke_help" );
00235     new KAction( i18n( "%1 &Web Site" ).arg( "KDEPrint" ), "network", 0, this, SLOT( slotHelp() ), m_actions, "invoke_web" );
00236 
00237     KActionMenu *mact = new KActionMenu(i18n("Pri&nter Tools"), "package_utilities", m_actions, "printer_tool");
00238     mact->setDelayed(false);
00239     connect(mact->popupMenu(), SIGNAL(activated(int)), SLOT(slotToolSelected(int)));
00240     QStringList files = KGlobal::dirs()->findAllResources("data", "kdeprint/tools/*.desktop");
00241     for (QStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
00242     {
00243         KSimpleConfig   conf(*it);
00244         conf.setGroup("Desktop Entry");
00245         mact->popupMenu()->insertItem(conf.readEntry("Name", "Unnamed"), mact->popupMenu()->count());
00246         m_toollist << conf.readEntry("X-KDE-Library");
00247     }
00248 
00249     // add actions to the toolbar
00250     m_actions->action("printer_add")->plug(m_toolbar);
00251     m_actions->action("printer_add_special")->plug(m_toolbar);
00252     m_toolbar->insertLineSeparator();
00253     m_actions->action("printer_state_change")->plug(m_toolbar);
00254     m_actions->action("printer_spool_change")->plug(m_toolbar);
00255     m_toolbar->insertSeparator();
00256     m_actions->action("printer_hard_default")->plug(m_toolbar);
00257     m_actions->action("printer_soft_default")->plug(m_toolbar);
00258     m_actions->action("printer_remove")->plug(m_toolbar);
00259     m_toolbar->insertSeparator();
00260     m_actions->action("printer_configure")->plug(m_toolbar);
00261     m_actions->action("printer_test")->plug(m_toolbar);
00262     m_actions->action("printer_tool")->plug(m_toolbar);
00263     m_pactionsindex = m_toolbar->insertSeparator();
00264     m_toolbar->insertLineSeparator();
00265     m_actions->action("server_restart")->plug(m_toolbar);
00266     m_actions->action("server_configure")->plug(m_toolbar);
00267     m_actions->action("enable_browsing")->plug(m_toolbar);
00268     m_actions->action("enable_sharing")->plug(m_toolbar);
00269     m_toolbar->insertLineSeparator();
00270     m_actions->action("manager_configure")->plug(m_toolbar);
00271     m_actions->action("view_refresh")->plug(m_toolbar);
00272     m_toolbar->insertLineSeparator();
00273     m_actions->action("view_printerinfos")->plug(m_toolbar);
00274     m_actions->action("view_change")->plug(m_toolbar);
00275     m_actions->action("orientation_change")->plug(m_toolbar);
00276     m_actions->action("view_pfilter")->plug(m_toolbar);
00277 
00278     // add actions to the menu bar
00279     QPopupMenu *menu = new QPopupMenu( this );
00280     m_actions->action( "printer_add" )->plug( menu );
00281     m_actions->action( "printer_add_special" )->plug( menu );
00282     //m_menubar->insertItem( i18n( "Add" ), menu );
00283     m_menubar->insertButton( "wizard", 0, true, i18n( "Add" ) );
00284     m_menubar->getButton( 0 )->setPopup( menu, true );
00285     menu = new QPopupMenu( this );
00286     m_actions->action("printer_state_change")->plug( menu );
00287     m_actions->action("printer_spool_change")->plug( menu );
00288     menu->insertSeparator();
00289     m_actions->action("printer_hard_default")->plug( menu );
00290     m_actions->action("printer_soft_default")->plug( menu );
00291     m_actions->action("printer_remove")->plug( menu );
00292     menu->insertSeparator();
00293     m_actions->action("printer_configure")->plug( menu );
00294     m_actions->action("printer_test")->plug( menu );
00295     m_actions->action("printer_tool")->plug( menu );
00296     menu->insertSeparator();
00297     //m_menubar->insertItem( i18n( "Printer" ), menu );
00298     m_menubar->insertButton( "printer1", 1, true, i18n( "Printer" ) );
00299     m_menubar->getButton( 1 )->setPopup( menu, true );
00300     menu = new QPopupMenu( this );
00301     m_actions->action("server_restart")->plug( menu );
00302     m_actions->action("server_configure")->plug( menu );
00303     m_actions->action("enable_browsing")->plug( menu );
00304     m_actions->action("enable_sharing")->plug( menu );
00305     //m_menubar->insertItem( i18n( "Server" ), menu );
00306     m_menubar->insertButton( "misc", 2, true, i18n( "Print Server" ) );
00307     m_menubar->getButton( 2 )->setPopup( menu, true );
00308     menu = new QPopupMenu( this );
00309     m_actions->action("manager_configure")->plug( menu );
00310     m_actions->action("view_refresh")->plug( menu );
00311     //m_menubar->insertItem( i18n( "Manager" ), menu );
00312     m_menubar->insertButton( "kdeprint_configmgr", 3, true, i18n( "Print Manager" ) );
00313     m_menubar->getButton( 3 )->setPopup( menu, true );
00314     menu = new QPopupMenu( this );
00315     m_actions->action("view_printerinfos")->plug( menu );
00316     m_actions->action("view_change")->plug( menu );
00317     m_actions->action("orientation_change")->plug( menu );
00318     m_actions->action( "view_toolbar" )->plug ( menu );
00319     m_actions->action( "view_menubar" )->plug ( menu );
00320     menu->insertSeparator();
00321     m_actions->action("view_pfilter")->plug( menu );
00322     //m_menubar->insertItem( i18n( "View" ), menu );
00323     m_menubar->insertButton( "view_remove", 4, true, i18n( "View" ) );
00324     m_menubar->getButton( 4 )->setPopup( menu, true );
00325     //m_menubar->setMinimumHeight( m_menubar->heightForWidth( 1000 ) );
00326     menu = new QPopupMenu( this );
00327     m_actions->action( "invoke_help" )->plug( menu );
00328     m_actions->action( "invoke_web" )->plug( menu );
00329     m_menubar->insertButton( "help", 5, true, i18n( "Documentation" ) );
00330     m_menubar->getButton( 5 )->setPopup( menu, true );
00331 
00332     loadPluginActions();
00333     slotPrinterSelected(QString::null);
00334 }
00335 
00336 void KMMainView::setBrowsingStatus() {
00337     enableBrowsing->setChecked(false);
00338     QProcess browsingStatus(QString("/usr/share/cups/browsing_status"), this, "browsingStatus");
00339     browsingStatus.start();
00340     while (browsingStatus.isRunning()) {
00341         kapp->processEvents();
00342     }
00343     int exitStatus = browsingStatus.exitStatus();
00344     if (exitStatus == 0) { // disabled
00345         enableBrowsing->setChecked(false);
00346     } else if (exitStatus == 1) { // enabled
00347         enableBrowsing->setChecked(true);
00348     } else if (exitStatus == 2) { // custom setup
00349         enableBrowsing->setEnabled(false);
00350     }
00351 }
00352 
00353 void KMMainView::setSharingStatus() {
00354     enableSharing->setChecked(false);
00355     QProcess sharingStatus(QString("/usr/share/cups/sharing_status"), this, "sharingStatus");
00356     sharingStatus.start();
00357     while (sharingStatus.isRunning()) {
00358         kapp->processEvents();
00359     }
00360     int exitStatus = sharingStatus.exitStatus();
00361     if (exitStatus == 0) { // disabled
00362         enableSharing->setChecked(false);
00363     } else if (exitStatus == 1) { // enabled
00364         enableSharing->setChecked(true);
00365     } else if (exitStatus == 2) { // custom setup
00366         enableSharing->setEnabled(false);
00367     }
00368 }
00369 
00370 void KMMainView::slotRefresh()
00371 {
00372     // TODO: remove me
00373 }
00374 
00375 void KMMainView::slotTimer()
00376 {
00377     kdDebug() << "KMMainView::slotTimer" << endl;
00378     QPtrList<KMPrinter> *printerlist = m_manager->printerList();
00379     bool ok = m_manager->errorMsg().isEmpty();
00380     m_printerview->setPrinterList(printerlist);
00381     if ( m_first )
00382     {
00383         if ( !ok )
00384             showErrorMsg(i18n("An error occurred while retrieving the printer list."));
00385         else
00386         {
00387             /* try to select the most appropriate printer:
00388              *    - soft default owner printer
00389              *    - hard default printer
00390              *    - first printer
00391              */
00392             QPtrListIterator<KMPrinter> it( *printerlist );
00393             KMPrinter *p1 = 0, *p2 = 0, *p3 = 0;
00394             while ( it.current() )
00395             {
00396                 if ( !it.current()->isVirtual() )
00397                 {
00398                     if ( it.current()->ownSoftDefault() )
00399                     {
00400                         p1 = it.current();
00401                         break;
00402                     }
00403                     else if ( it.current()->isHardDefault() )
00404                         p2 = it.current();
00405                     else if ( !p3 )
00406                         p3 = it.current();
00407                 }
00408                 ++it;
00409             }
00410             if ( p1 || p2 || p3 )
00411                 m_printerview->setPrinter( p1 ? p1 : ( p2 ? p2 : p3 ) );
00412         }
00413         m_first = false;
00414     }
00415 }
00416 
00417 void KMMainView::slotPrinterSelected(const QString& prname)
00418 {
00419     KMPrinter   *p = KMManager::self()->findPrinter(prname);
00420     m_current = p;
00421     if (p && !p->isSpecial())
00422         KMFactory::self()->manager()->completePrinter(p);
00423     m_printerpages->setPrinter(p);
00424 
00425     // update actions state (only if toolbar enabled, workaround for toolbar
00426     // problem).
00427     //if (m_toolbar->isEnabled())
00428     //{
00429         int     mask = (m_manager->hasManagement() ? m_manager->printerOperationMask() : 0);
00430         bool    sp = !(p && p->isSpecial());
00431         m_actions->action("printer_remove")->setEnabled(!sp || ((mask & KMManager::PrinterRemoval) && p && p->isLocal() && !p->isImplicit()));
00432         m_actions->action("printer_configure")->setEnabled(!sp || ((mask & KMManager::PrinterConfigure) && p && !p->isClass(true) /*&& p->isLocal()*/));
00433         m_actions->action("printer_hard_default")->setEnabled((sp && (mask & KMManager::PrinterDefault) && p && !p->isClass(true) && !p->isHardDefault() && p->isLocal()));
00434         m_actions->action("printer_soft_default")->setEnabled((p && !p->isSoftDefault()));
00435         m_actions->action("printer_test")->setEnabled((sp && (mask & KMManager::PrinterTesting) && p && !p->isClass(true)));
00436         bool    stmask = (sp && (mask & KMManager::PrinterEnabling) && p);
00437         m_actions->action("printer_state_change")->setEnabled(stmask && p->isLocal());
00438         m_actions->action("printer_spool_change")->setEnabled(stmask);
00439         m_actions->action("printer_start")->setEnabled((stmask && p->state() == KMPrinter::Stopped));
00440         m_actions->action("printer_stop")->setEnabled((stmask && p->state() != KMPrinter::Stopped));
00441         m_actions->action("printer_enable")->setEnabled((stmask && !p->acceptJobs()));
00442         m_actions->action("printer_disable")->setEnabled((stmask && p->acceptJobs()));
00443 
00444         m_actions->action("printer_add")->setEnabled((mask & KMManager::PrinterCreation));
00445         mask = m_manager->serverOperationMask();
00446         m_actions->action("server_restart")->setEnabled((mask & KMManager::ServerRestarting));
00447         m_actions->action("server_configure")->setEnabled((mask & KMManager::ServerConfigure));
00448 
00449         KMFactory::self()->manager()->validatePluginActions(m_actions, p);
00450     //}
00451     m_actions->action("printer_tool")->setEnabled(p && !p->isClass(true) && !p->isRemote() && !p->isSpecial());
00452 }
00453 
00454 void KMMainView::setViewType(int ID)
00455 {
00456     ((KSelectAction*)m_actions->action("view_change"))->setCurrentItem(ID);
00457     slotChangeView(ID);
00458 }
00459 
00460 int KMMainView::viewType() const
00461 { return m_printerview->viewType(); }
00462 
00463 void KMMainView::slotChangeView(int ID)
00464 {
00465     kdDebug() << "KMMainView::slotChangeView" << endl;
00466     if (ID >= KMPrinterView::Icons && ID <= KMPrinterView::Tree)
00467         m_printerview->setViewType((KMPrinterView::ViewType)ID);
00468 }
00469 
00470 void KMMainView::slotRightButtonClicked(const QString& prname, const QPoint& p)
00471 {
00472     KMPrinter   *printer = KMManager::self()->findPrinter(prname);
00473     // construct popup menu
00474     m_pop->clear();
00475     if (printer)
00476     {
00477         m_current = printer;
00478         if (!printer->isSpecial())
00479         {
00480             if (printer->isLocal())
00481                 m_actions->action((printer->state() == KMPrinter::Stopped ? "printer_start" : "printer_stop"))->plug(m_pop);
00482             m_actions->action((printer->acceptJobs() ? "printer_disable" : "printer_enable"))->plug(m_pop);
00483             m_pop->insertSeparator();
00484         }
00485         if (!printer->isSoftDefault()) m_actions->action("printer_soft_default")->plug(m_pop);
00486         if (printer->isLocal() && !printer->isImplicit())
00487         {
00488             if (!printer->isHardDefault()) m_actions->action("printer_hard_default")->plug(m_pop);
00489             m_actions->action("printer_remove")->plug(m_pop);
00490             m_pop->insertSeparator();
00491             if (!printer->isClass(true))
00492             {
00493                 m_actions->action("printer_configure")->plug(m_pop);
00494                 m_actions->action("printer_test")->plug(m_pop);
00495                 m_actions->action("printer_tool")->plug(m_pop);
00496                 m_pop->insertSeparator();
00497             }
00498         }
00499         else
00500         {
00501             if (!printer->isClass(true))
00502             {
00503                 m_actions->action("printer_configure")->plug(m_pop);
00504                 m_actions->action("printer_test")->plug(m_pop);
00505             }
00506             m_pop->insertSeparator();
00507         }
00508         if (!printer->isSpecial())
00509         {
00510             QValueList<KAction*>    pactions = m_actions->actions("plugin");
00511             for (QValueList<KAction*>::Iterator it=pactions.begin(); it!=pactions.end(); ++it)
00512                 (*it)->plug(m_pop);
00513             if (pactions.count() > 0)
00514                 m_pop->insertSeparator();
00515         }
00516     }
00517     else
00518     {
00519         m_actions->action("printer_add")->plug(m_pop);
00520         m_actions->action("printer_add_special")->plug(m_pop);
00521         m_pop->insertSeparator();
00522         m_actions->action("server_restart")->plug(m_pop);
00523         m_actions->action("server_configure")->plug(m_pop);
00524         m_actions->action("enable_browsing")->plug(m_pop);
00525         m_actions->action("enable_sharing")->plug(m_pop);
00526         m_pop->insertSeparator();
00527         m_actions->action("manager_configure")->plug(m_pop);
00528         m_actions->action("view_refresh")->plug(m_pop);
00529         m_pop->insertSeparator();
00530     }
00531     m_actions->action("view_printerinfos")->plug(m_pop);
00532     m_actions->action("view_change")->plug(m_pop);
00533     m_actions->action("orientation_change")->plug(m_pop);
00534     m_actions->action("view_toolbar")->plug(m_pop);
00535     m_actions->action("view_menubar")->plug(m_pop);
00536     m_pop->insertSeparator();
00537     m_actions->action("view_pfilter")->plug(m_pop);
00538 
00539     // pop the menu
00540     m_pop->popup(p);
00541 }
00542 
00543 void KMMainView::slotChangePrinterState()
00544 {
00545     QString opname = sender()->name();
00546     if (m_current && opname.startsWith("printer_"))
00547     {
00548         opname = opname.mid(8);
00549         KMTimer::self()->hold();
00550         bool    result(false);
00551         if (opname == "enable")
00552             result = m_manager->enablePrinter(m_current, true);
00553         else if (opname == "disable")
00554             result = m_manager->enablePrinter(m_current, false);
00555         else if (opname == "start")
00556             result = m_manager->startPrinter(m_current, true);
00557         else if (opname == "stop")
00558             result = m_manager->startPrinter(m_current, false);
00559         if (!result)
00560             showErrorMsg(i18n("Unable to modify the state of printer %1.").arg(m_current->printerName()));
00561         KMTimer::self()->release(result);
00562     }
00563 }
00564 
00565 void KMMainView::slotRemove()
00566 {
00567     if (m_current)
00568     {
00569         KMTimer::self()->hold();
00570         bool    result(false);
00571         if (KMessageBox::warningYesNo(this,i18n("Do you really want to remove %1?").arg(m_current->printerName())) == KMessageBox::Yes)
00572             if (m_current->isSpecial())
00573             {
00574                 if (!(result=m_manager->removeSpecialPrinter(m_current)))
00575                     showErrorMsg(i18n("Unable to remove special printer %1.").arg(m_current->printerName()));
00576             }
00577             else if (!(result=m_manager->removePrinter(m_current)))
00578                 showErrorMsg(i18n("Unable to remove printer %1.").arg(m_current->printerName()));
00579         KMTimer::self()->release(result);
00580     }
00581 }
00582 
00583 void KMMainView::slotConfigure()
00584 {
00585     if (m_current)
00586     {
00587         KMTimer::self()->hold();
00588         bool    needRefresh(false);
00589         if (m_current->isSpecial())
00590         {
00591             KMSpecialPrinterDlg dlg(this);
00592             dlg.setPrinter(m_current);
00593             if (dlg.exec())
00594             {
00595                 KMPrinter   *prt = dlg.printer();
00596                 if (prt->name() != m_current->name())
00597                     m_manager->removeSpecialPrinter(m_current);
00598                 m_manager->createSpecialPrinter(prt);
00599                 needRefresh = true;
00600             }
00601         }
00602         else
00603         {
00604             DrMain  *driver = m_manager->loadPrinterDriver(m_current, true);
00605             if (driver)
00606             {
00607                 KMDriverDialog  dlg(this);
00608                 dlg.setCaption(i18n("Configure %1").arg(m_current->printerName()));
00609                 dlg.setDriver(driver);
00610                 // disable OK button for remote printer (read-only dialog)
00611                 if (m_current->isRemote())
00612                     dlg.enableButtonOK(false);
00613                 if (dlg.exec())
00614                     if (!m_manager->savePrinterDriver(m_current,driver))
00615                         showErrorMsg(i18n("Unable to modify settings of printer %1.").arg(m_current->printerName()));
00616                 delete driver;
00617             }
00618             else
00619                 showErrorMsg(i18n("Unable to load a valid driver for printer %1.").arg(m_current->printerName()));
00620         }
00621         KMTimer::self()->release(needRefresh);
00622     }
00623 }
00624 
00625 void KMMainView::slotAdd()
00626 {
00627     KMTimer::self()->hold();
00628 
00629     int result(0);
00630     if ((result=kdeprint_management_add_printer_wizard(this)) == -1)
00631         showErrorMsg(i18n("Unable to create printer."));
00632 
00633     KMTimer::self()->release((result == 1));
00634 }
00635 
00636 void KMMainView::slotHardDefault()
00637 {
00638     if (m_current)
00639     {
00640         KMTimer::self()->hold();
00641         bool    result = m_manager->setDefaultPrinter(m_current);
00642         if (!result)
00643             showErrorMsg(i18n("Unable to define printer %1 as default.").arg(m_current->printerName()));
00644         KMTimer::self()->release(result);
00645     }
00646 }
00647 
00648 void KMMainView::slotSoftDefault()
00649 {
00650     if (m_current)
00651     {
00652         KMTimer::self()->hold();
00653         KMFactory::self()->virtualManager()->setAsDefault(m_current,QString::null);
00654         KMTimer::self()->release(true);
00655     }
00656 }
00657 
00658 void KMMainView::setOrientation(int o)
00659 {
00660     int     ID = (o == Qt::Horizontal ? 1 : 0);
00661     ((KSelectAction*)m_actions->action("orientation_change"))->setCurrentItem(ID);
00662     slotChangeDirection(ID);
00663 }
00664 
00665 int KMMainView::orientation() const
00666 { return (m_boxlayout->direction() == QBoxLayout::LeftToRight ? Qt::Horizontal : Qt::Vertical);  }
00667 
00668 void KMMainView::slotChangeDirection(int d)
00669 {
00670     m_boxlayout->setDirection(d == 1 ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom);
00671 }
00672 
00673 void KMMainView::slotTest()
00674 {
00675     if (m_current)
00676     {
00677         KMTimer::self()->hold();
00678         if (KMessageBox::warningContinueCancel(this, i18n("You are about to print a test page on %1. Do you want to continue?").arg(m_current->printerName()), QString::null, i18n("Print Test Page"), "printTestPage") == KMessageBox::Continue)
00679         {
00680             if (KMFactory::self()->manager()->testPrinter(m_current))
00681                 KMessageBox::information(this,i18n("Test page successfully sent to printer %1.").arg(m_current->printerName()));
00682             else
00683                 showErrorMsg(i18n("Unable to test printer %1.").arg(m_current->printerName()));
00684         }
00685         KMTimer::self()->release(true);
00686     }
00687 }
00688 
00689 void KMMainView::showErrorMsg(const QString& msg, bool usemgr)
00690 {
00691     QString s(msg);
00692     if (usemgr)
00693     {
00694         s.prepend("<p>");
00695         s.append(" ");
00696         s += i18n("Error message received from manager:</p><p>%1</p>");
00697         if (m_manager->errorMsg().isEmpty())
00698             s = s.arg(i18n("Internal error (no error message)."));
00699         else
00700             s = s.arg(m_manager->errorMsg());
00701         // clean up error message
00702         m_manager->setErrorMsg(QString::null);
00703     }
00704     s.prepend("<qt>").append("</qt>");
00705     KMTimer::self()->hold();
00706     KMessageBox::error(this,s);
00707     KMTimer::self()->release();
00708 }
00709 
00710 void KMMainView::slotServerRestart()
00711 {
00712     KMTimer::self()->hold();
00713     bool    result = m_manager->restartServer();
00714     if (!result)
00715     {
00716         showErrorMsg(i18n("Unable to restart print server."));
00717         KMTimer::self()->release( false );
00718     }
00719     else
00720     {
00721         reset( i18n( "Restarting server..." ), false, false );
00722     }
00723 }
00724 
00725 void KMMainView::slotServerConfigure()
00726 {
00727     KMTimer::self()->hold();
00728     bool    result = m_manager->configureServer(this);
00729     if (!result)
00730     {
00731         showErrorMsg(i18n("Unable to configure print server."));
00732         KMTimer::self()->release( false );
00733     }
00734     else
00735     {
00736         reset( i18n( "Configuring server..." ), false, false );
00737     }
00738 }
00739 
00740 void KMMainView::slotEnableBrowsing()
00741 {
00742     setCursor(QCursor(Qt::BusyCursor));
00743     
00744     QString scaryMessage = i18n("Enabling local browsing for printers will open a network port (631) on your computer.  If security problems are discovered in the print server, remote attackers could access your computer as the \"cupsys\" user.");
00745 
00746     if (enableBrowsing->isChecked()) {
00747         if (KMessageBox::warningYesNo( this, scaryMessage, i18n("Access Printers on Local Network"), KGuiItem(i18n("Enable Browsing")), KGuiItem(i18n("Don't Enable Browsing")) ) == KMessageBox::Yes) {
00748             KProcess *proc = new KProcess;
00749             *proc << "kdesu";
00750                 *proc << "/usr/share/cups/enable_browsing 1";
00751             proc->start(KProcess::Block);
00752             setBrowsingStatus();
00753         } else {
00754             enableBrowsing->setChecked(false);
00755         }
00756     } else {
00757         KProcess *proc = new KProcess;
00758         *proc << "kdesu";
00759         *proc << "/usr/share/cups/enable_browsing 0";
00760         proc->start(KProcess::Block);
00761         setBrowsingStatus();
00762     }
00763     setCursor(QCursor(Qt::ArrowCursor));
00764 }
00765 
00766 void KMMainView::slotEnableSharing()
00767 {
00768     setCursor(QCursor(Qt::BusyCursor));
00769     
00770     QString scaryMessage = i18n("Enabling local browsing for printers will open a network port (631) on your computer.  If security problems are discovered in the print server, remote attackers could access your computer as the \"cupsys\" user.");
00771 
00772     if (enableSharing->isChecked()) {
00773         if (KMessageBox::warningYesNo( this, scaryMessage, i18n("Share Printers on Local Network"), KGuiItem(i18n("Enable Sharing")), KGuiItem(i18n("Don't Enable Sharing")) ) == KMessageBox::Yes) {
00774             KProcess *proc = new KProcess;
00775             *proc << "kdesu";
00776                 *proc << "/usr/share/cups/enable_sharing 1";
00777             proc->start(KProcess::Block);
00778             setSharingStatus();
00779         } else {
00780             enableSharing->setChecked(false);
00781         }
00782     } else {
00783         KProcess *proc = new KProcess;
00784         *proc << "kdesu";
00785         *proc << "/usr/share/cups/enable_sharing 0";
00786         proc->start(KProcess::Block);
00787         setSharingStatus();
00788     }
00789     setCursor(QCursor(Qt::ArrowCursor));
00790 }
00791 
00792 void KMMainView::slotToggleToolBar(bool on)
00793 {
00794     if (on) m_toolbar->show();
00795     else m_toolbar->hide();
00796 }
00797 
00798 void KMMainView::slotToggleMenuBar( bool on )
00799 {
00800     if ( on )
00801         m_menubar->show();
00802     else
00803         m_menubar->hide();
00804 }
00805 
00806 void KMMainView::slotManagerConfigure()
00807 {
00808     KMTimer::self()->hold();
00809     KMConfigDialog  dlg(this,"ConfigDialog");
00810     if ( dlg.exec() )
00811     {
00812         loadParameters();
00813     }
00814     /* when "OK":
00815      *  => the config file is saved
00816      *  => triggering a DCOP signal
00817      *  => configChanged() called
00818      * hence no need to refresh, just release the timer
00819      */
00820     KMTimer::self()->release( false );
00821 }
00822 
00823 void KMMainView::slotAddSpecial()
00824 {
00825     KMTimer::self()->hold();
00826     KMSpecialPrinterDlg dlg(this);
00827     if (dlg.exec())
00828     {
00829         KMPrinter   *prt = dlg.printer();
00830         m_manager->createSpecialPrinter(prt);
00831     }
00832     KMTimer::self()->release(true);
00833 }
00834 
00835 void KMMainView::slotShowPrinterInfos(bool on)
00836 {
00837     if (on)
00838         m_printerpages->show();
00839     else
00840         m_printerpages->hide();
00841     m_actions->action("orientation_change")->setEnabled(on);
00842 }
00843 
00844 void KMMainView::enableToolbar(bool on)
00845 {
00846     KToggleAction   *act = (KToggleAction*)m_actions->action("view_toolbar");
00847     m_toolbar->setEnabled(on);
00848     act->setEnabled(on);
00849     if (on && act->isChecked())
00850         m_toolbar->show();
00851     else
00852         m_toolbar->hide();
00853 }
00854 
00855 KAction* KMMainView::action(const char *name)
00856 {
00857     return m_actions->action(name);
00858 }
00859 
00860 /*
00861 void KMMainView::aboutToReload()
00862 {
00863     m_printerview->setPrinterList(0);
00864 }
00865 */
00866 
00867 void KMMainView::reload()
00868 {
00869     removePluginActions();
00870     loadPluginActions();
00871 
00872     // redo the connection as the old manager object has been removed
00873     connect( m_manager, SIGNAL( updatePossible( bool ) ), SLOT( slotUpdatePossible( bool ) ) );
00874 
00875     // We must delay the refresh such that all objects has been
00876     // correctly reloaded (otherwise, crash in KMJobViewer).
00877     reset( i18n( "Initializing manager..." ), true, true );
00878 }
00879 
00880 void KMMainView::showPrinterInfos(bool on)
00881 {
00882     static_cast<KToggleAction*>(m_actions->action("view_printerinfos"))->setChecked(on);
00883     slotShowPrinterInfos(on);
00884 }
00885 
00886 bool KMMainView::printerInfosShown() const
00887 {
00888     return (static_cast<KToggleAction*>(m_actions->action("view_printerinfos"))->isChecked());
00889 }
00890 
00891 void KMMainView::loadPluginActions()
00892 {
00893     KMFactory::self()->manager()->createPluginActions(m_actions);
00894     QValueList<KAction*>    pactions = m_actions->actions("plugin");
00895     int index = m_pactionsindex;
00896     //QPopupMenu *menu = m_menubar->findItem( m_menubar->idAt( 1 ) )->popup();
00897     QPopupMenu *menu = m_menubar->getButton( 1 )->popup();
00898     for (QValueList<KAction*>::Iterator it=pactions.begin(); it!=pactions.end(); ++it)
00899     {
00900         (*it)->plug(m_toolbar, index++);
00901         ( *it )->plug( menu );
00902     }
00903 }
00904 
00905 void KMMainView::removePluginActions()
00906 {
00907     QValueList<KAction*>    pactions = m_actions->actions("plugin");
00908     for (QValueList<KAction*>::Iterator it=pactions.begin(); it!=pactions.end(); ++it)
00909     {
00910         (*it)->unplugAll();
00911         delete (*it);
00912     }
00913 }
00914 
00915 void KMMainView::slotToolSelected(int ID)
00916 {
00917     KMTimer::self()->hold();
00918 
00919     QString libname = m_toollist[ID];
00920     libname.prepend("kdeprint_tool_");
00921     if (m_current && !m_current->device().isEmpty() && !libname.isEmpty())
00922     {
00923         KLibFactory *factory = KLibLoader::self()->factory(libname.local8Bit());
00924         if (factory)
00925         {
00926             QStringList args;
00927             args << m_current->device() << m_current->printerName();
00928             KDialogBase *dlg = static_cast<KDialogBase*>(factory->create(this, "Tool", 0, args));
00929             if (dlg)
00930                 dlg->exec();
00931             delete dlg;
00932         }
00933     }
00934     else
00935         KMessageBox::error(this,
00936             i18n("Unable to start printer tool. Possible reasons are: "
00937                  "no printer selected, the selected printer doesn't have "
00938                  "any local device defined (printer port), or the tool library "
00939                  "could not be found."));
00940 
00941     KMTimer::self()->release();
00942 }
00943 
00944 void KMMainView::slotToggleFilter(bool on)
00945 {
00946     KMTimer::self()->hold();
00947     KMManager::self()->enableFilter(on);
00948     KMTimer::self()->release(true);
00949 }
00950 
00951 void KMMainView::configChanged()
00952 {
00953     reset( i18n( "Initializing manager..." ), false, true );
00954 }
00955 
00956 void KMMainView::slotUpdatePossible( bool flag )
00957 {
00958     destroyMessageWindow();
00959     if ( !flag )
00960         showErrorMsg( i18n( "Unable to retrieve the printer list." ) );
00961     KMTimer::self()->release( true );
00962 }
00963 
00964 void KMMainView::createMessageWindow( const QString& txt, int delay )
00965 {
00966     destroyMessageWindow();
00967     MessageWindow::add( m_printerview, txt, delay );
00968 }
00969 
00970 void KMMainView::destroyMessageWindow()
00971 {
00972     MessageWindow::remove( m_printerview );
00973 }
00974 
00975 void KMMainView::slotInit()
00976 {
00977     reset( i18n( "Initializing manager..." ), true, true );
00978 }
00979 
00980 void KMMainView::reset( const QString& msg, bool useDelay, bool holdTimer )
00981 {
00982     if ( holdTimer )
00983         KMTimer::self()->hold();
00984     m_printerview->setPrinterList( 0 );
00985     if ( !msg.isEmpty() )
00986         createMessageWindow( msg, ( useDelay ? 500 : 0 ) );
00987     m_first = true;
00988     m_manager->checkUpdatePossible();
00989 }
00990 
00991 void KMMainView::slotHelp()
00992 {
00993     QString s = sender()->name();
00994     if ( s == "invoke_help" )
00995         kapp->invokeHelp( QString::null, "kdeprint" );
00996     else if ( s == "invoke_web" )
00997     {
00998         QStringList args;
00999         args << "exec" << "http://printing.kde.org";
01000         kapp->kdeinitExec( "kfmclient", args );
01001     }
01002     else
01003         kdDebug( 500 ) << "Unknown help invokator: " << s << endl;
01004 }
01005 
01006 #include "kmmainview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys