kdeprint Library API Documentation

kmmainview.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <goffioul@imec.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., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, 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 
00059 #undef m_manager
00060 #define m_manager   KMFactory::self()->manager()
00061 
00062 extern "C"
00063 {
00064     int add_printer_wizard(QWidget *parent)
00065     {
00066         KMWizard    dlg(parent);
00067         int     flag(0);
00068         if (dlg.exec())
00069         {
00070             flag = 1;
00071             // check if the printer already exists, and ask confirmation if needed.
00072             if (KMFactory::self()->manager()->findPrinter(dlg.printer()->name()) != 0)
00073                 if (KMessageBox::warningYesNo(parent,i18n("The printer %1 already exists. Continuing will overwrite existing printer. Do you want to continue?").arg(dlg.printer()->name())) == KMessageBox::No)
00074                     flag = 0;
00075             // try to add printer only if flag is true.
00076             if (flag && !KMFactory::self()->manager()->createPrinter(dlg.printer()))
00077                 flag = -1;
00078         }
00079         return flag;
00080     }
00081 
00082     bool config_dialog(QWidget *parent)
00083     {
00084         KMConfigDialog  dlg(parent);
00085         return dlg.exec();
00086     }
00087 }
00088 
00089 KMMainView::KMMainView(QWidget *parent, const char *name, KActionCollection *coll)
00090 : QWidget(parent, name)
00091 {
00092     m_current = 0;
00093     m_first = true;
00094 
00095     // create widgets
00096     m_printerview = new KMPrinterView(this, "PrinterView");
00097     m_printerpages = new KMPages(this, "PrinterPages");
00098     m_pop = new QPopupMenu(this);
00099     m_toolbar = new KToolBar(this, "ToolBar");
00100     m_toolbar->setMovingEnabled(false);
00101     m_plugin = new PluginComboBox(this, "Plugin");
00102     /*
00103     m_menubar = new KMenuBar( this );
00104     static_cast<KMenuBar*>( m_menubar )->setTopLevelMenu( false );
00105     */
00106     m_menubar = new KToolBar( this, "MenuBar", false, false );
00107     m_menubar->setIconText( KToolBar::IconTextRight );
00108     m_menubar->setMovingEnabled( false );
00109 
00110     // layout
00111     QVBoxLayout *m_layout = new QVBoxLayout(this, 0, 0);
00112     m_layout->addWidget(m_toolbar);
00113     m_layout->addWidget( m_menubar );
00114     m_boxlayout = new QBoxLayout(QBoxLayout::TopToBottom, 0, 0);
00115     m_layout->addLayout(m_boxlayout);
00116     m_boxlayout->addWidget(m_printerview);
00117     m_boxlayout->addWidget(m_printerpages);
00118     m_layout->addSpacing(5);
00119     m_layout->addWidget(m_plugin, 0);
00120 
00121     // connections
00122     connect(KMTimer::self(),SIGNAL(timeout()),SLOT(slotTimer()));
00123     connect(m_printerview,SIGNAL(printerSelected(const QString&)),SLOT(slotPrinterSelected(const QString&)));
00124     connect(m_printerview,SIGNAL(rightButtonClicked(const QString&,const QPoint&)),SLOT(slotRightButtonClicked(const QString&,const QPoint&)));
00125     connect(m_pop,SIGNAL(aboutToShow()),KMTimer::self(),SLOT(hold()));
00126     connect(m_pop,SIGNAL(aboutToHide()),KMTimer::self(),SLOT(release()));
00127     connect( m_manager, SIGNAL( updatePossible( bool ) ), SLOT( slotUpdatePossible( bool ) ) );
00128 
00129     // actions
00130     if (coll)
00131         m_actions = coll;
00132     else
00133         m_actions = new KActionCollection(this);
00134     initActions();
00135 
00136     // first update
00137     restoreSettings();
00138     loadParameters();
00139 
00140     //slotRefresh();
00141     //KMTimer::self()->release(true);
00142     KMTimer::self()->hold();
00143     createMessageWindow( i18n( "Initializing manager..." ) );
00144     m_manager->checkUpdatePossible();
00145 }
00146 
00147 KMMainView::~KMMainView()
00148 {
00149     saveSettings();
00150     //KMFactory::release();
00151 }
00152 
00153 void KMMainView::loadParameters()
00154 {
00155 }
00156 
00157 void KMMainView::restoreSettings()
00158 {
00159     KConfig *conf = KMFactory::self()->printConfig();
00160     conf->setGroup("General");
00161     setViewType((KMPrinterView::ViewType)conf->readNumEntry("ViewType",KMPrinterView::Icons));
00162     setOrientation(conf->readNumEntry("Orientation", Qt::Vertical));
00163     bool    view = conf->readBoolEntry("ViewToolBar",false);
00164     slotToggleToolBar(view);
00165     ((KToggleAction*)m_actions->action("view_toolbar"))->setChecked(view);
00166     view = conf->readBoolEntry( "ViewMenuBar", true );
00167     slotToggleMenuBar( view );
00168     static_cast<KToggleAction*>( m_actions->action( "view_menubar" ) )->setChecked( view );
00169     view = conf->readBoolEntry("ViewPrinterInfos",true);
00170     slotShowPrinterInfos(view);
00171     ((KToggleAction*)m_actions->action("view_printerinfos"))->setChecked(view);
00172 }
00173 
00174 void KMMainView::saveSettings()
00175 {
00176     KConfig *conf = KMFactory::self()->printConfig();
00177     conf->setGroup("General");
00178     conf->writeEntry("ViewType",(int)m_printerview->viewType());
00179     conf->writeEntry("Orientation",(int)orientation());
00180     conf->writeEntry("ViewToolBar",((KToggleAction*)m_actions->action("view_toolbar"))->isChecked());
00181     conf->writeEntry("ViewMenuBar",static_cast<KToggleAction*>( m_actions->action("view_menubar") )->isChecked());
00182     conf->writeEntry("ViewPrinterInfos",((KToggleAction*)m_actions->action("view_printerinfos"))->isChecked());
00183     conf->sync();
00184 }
00185 
00186 void KMMainView::initActions()
00187 {
00188     KIconSelectAction   *vact = new KIconSelectAction(i18n("&View"),0,m_actions,"view_change");
00189     QStringList iconlst;
00190     iconlst << "view_icon" << "view_detailed" << "view_tree";
00191     vact->setItems(QStringList::split(',',i18n("&Icons,&List,&Tree"),false), iconlst);
00192     vact->setCurrentItem(0);
00193     connect(vact,SIGNAL(activated(int)),SLOT(slotChangeView(int)));
00194 
00195     KActionMenu *stateAct = new KActionMenu(i18n("Start/Stop Printer"), "kdeprint_printstate", m_actions, "printer_state_change");
00196     stateAct->setDelayed(false);
00197     stateAct->insert(new KAction(i18n("&Start Printer"),"kdeprint_enableprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_start"));
00198     stateAct->insert(new KAction(i18n("Sto&p Printer"),"kdeprint_stopprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_stop"));
00199 
00200     stateAct = new KActionMenu(i18n("Enable/Disable Job Spooling"), "kdeprint_queuestate", m_actions, "printer_spool_change");
00201     stateAct->setDelayed(false);
00202     stateAct->insert(new KAction(i18n("&Enable Job Spooling"),"kdeprint_enableprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_enable"));
00203     stateAct->insert(new KAction(i18n("&Disable Job Spooling"),"kdeprint_stopprinter",0,this,SLOT(slotChangePrinterState()),m_actions,"printer_disable"));
00204 
00205     new KAction(i18n("&Remove"),"edittrash",0,this,SLOT(slotRemove()),m_actions,"printer_remove");
00206     new KAction(i18n("&Configure..."),"configure",0,this,SLOT(slotConfigure()),m_actions,"printer_configure");
00207     new KAction(i18n("Add &Printer/Class..."),"kdeprint_addprinter",0,this,SLOT(slotAdd()),m_actions,"printer_add");
00208     new KAction(i18n("Add &Special (pseudo) Printer..."),"kdeprint_addpseudo",0,this,SLOT(slotAddSpecial()),m_actions,"printer_add_special");
00209     new KAction(i18n("Set as &Local Default"),"kdeprint_defaulthard",0,this,SLOT(slotHardDefault()),m_actions,"printer_hard_default");
00210     new KAction(i18n("Set as &User Default"),"kdeprint_defaultsoft",0,this,SLOT(slotSoftDefault()),m_actions,"printer_soft_default");
00211     new KAction(i18n("&Test Printer..."),"kdeprint_testprinter",0,this,SLOT(slotTest()),m_actions,"printer_test");
00212     new KAction(i18n("Configure &Manager..."),"kdeprint_configmgr",0,this,SLOT(slotManagerConfigure()),m_actions,"manager_configure");
00213     new KAction(i18n("Initialize Manager/&View"),"reload",0,this,SLOT(slotInit()),m_actions,"view_refresh");
00214 
00215     KIconSelectAction   *dact = new KIconSelectAction(i18n("&Orientation"),0,m_actions,"orientation_change");
00216     iconlst.clear();
00217     iconlst << "view_top_bottom" << "view_left_right";
00218     dact->setItems(QStringList::split(',',i18n("&Vertical,&Horizontal"),false), iconlst);
00219     dact->setCurrentItem(0);
00220     connect(dact,SIGNAL(activated(int)),SLOT(slotChangeDirection(int)));
00221 
00222     new KAction(i18n("R&estart Server"),"kdeprint_restartsrv",0,this,SLOT(slotServerRestart()),m_actions,"server_restart");
00223     new KAction(i18n("Configure &Server..."),"kdeprint_configsrv",0,this,SLOT(slotServerConfigure()),m_actions,"server_configure");
00224 
00225     KToggleAction   *tact = new KToggleAction(i18n("View &Toolbar"),0,m_actions,"view_toolbar");
00226     connect(tact,SIGNAL(toggled(bool)),SLOT(slotToggleToolBar(bool)));
00227     tact = new KToggleAction( i18n( "View Me&nu Toolbar" ), 0, m_actions, "view_menubar" );
00228     connect( tact, SIGNAL( toggled( bool ) ), SLOT( slotToggleMenuBar( bool ) ) );
00229     tact = new KToggleAction(i18n("Show/Hide Pr&inter Details"),"kdeprint_printer_infos", 0,m_actions,"view_printerinfos");
00230     tact->setChecked(true);
00231     connect(tact,SIGNAL(toggled(bool)),SLOT(slotShowPrinterInfos(bool)));
00232 
00233     tact = new KToggleAction(i18n("Toggle Printer &Filtering"), "filter", 0, m_actions, "view_pfilter");
00234     tact->setChecked(KMManager::self()->isFilterEnabled());
00235     connect(tact, SIGNAL(toggled(bool)), SLOT(slotToggleFilter(bool)));
00236 
00237     new KAction( i18n( "%1 &Handbook" ).arg( "KDEPrint" ), "contents", 0, this, SLOT( slotHelp() ), m_actions, "invoke_help" );
00238     new KAction( i18n( "%1 &Web Site" ).arg( "KDEPrint" ), "network", 0, this, SLOT( slotHelp() ), m_actions, "invoke_web" );
00239 
00240     KActionMenu *mact = new KActionMenu(i18n("Pri&nter Tools"), "package_utilities", m_actions, "printer_tool");
00241     mact->setDelayed(false);
00242     connect(mact->popupMenu(), SIGNAL(activated(int)), SLOT(slotToolSelected(int)));
00243     QStringList files = KGlobal::dirs()->findAllResources("data", "kdeprint/tools/*.desktop");
00244     for (QStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
00245     {
00246         KSimpleConfig   conf(*it);
00247         conf.setGroup("Desktop Entry");
00248         mact->popupMenu()->insertItem(conf.readEntry("Name", "Unnamed"), mact->popupMenu()->count());
00249         m_toollist << conf.readEntry("X-KDE-Library");
00250     }
00251 
00252     // add actions to the toolbar
00253     m_actions->action("printer_add")->plug(m_toolbar);
00254     m_actions->action("printer_add_special")->plug(m_toolbar);
00255     m_toolbar->insertLineSeparator();
00256     m_actions->action("printer_state_change")->plug(m_toolbar);
00257     m_actions->action("printer_spool_change")->plug(m_toolbar);
00258     m_toolbar->insertSeparator();
00259     m_actions->action("printer_hard_default")->plug(m_toolbar);
00260     m_actions->action("printer_soft_default")->plug(m_toolbar);
00261     m_actions->action("printer_remove")->plug(m_toolbar);
00262     m_toolbar->insertSeparator();
00263     m_actions->action("printer_configure")->plug(m_toolbar);
00264     m_actions->action("printer_test")->plug(m_toolbar);
00265     m_actions->action("printer_tool")->plug(m_toolbar);
00266     m_pactionsindex = m_toolbar->insertSeparator();
00267     m_toolbar->insertLineSeparator();
00268     m_actions->action("server_restart")->plug(m_toolbar);
00269     m_actions->action("server_configure")->plug(m_toolbar);
00270     m_toolbar->insertLineSeparator();
00271     m_actions->action("manager_configure")->plug(m_toolbar);
00272     m_actions->action("view_refresh")->plug(m_toolbar);
00273     m_toolbar->insertLineSeparator();
00274     m_actions->action("view_printerinfos")->plug(m_toolbar);
00275     m_actions->action("view_change")->plug(m_toolbar);
00276     m_actions->action("orientation_change")->plug(m_toolbar);
00277     m_actions->action("view_pfilter")->plug(m_toolbar);
00278 
00279     // add actions to the menu bar
00280     QPopupMenu *menu = new QPopupMenu( this );
00281     m_actions->action( "printer_add" )->plug( menu );
00282     m_actions->action( "printer_add_special" )->plug( menu );
00283     //m_menubar->insertItem( i18n( "Add" ), menu );
00284     m_menubar->insertButton( "wizard", 0, true, i18n( "Add" ) );
00285     m_menubar->getButton( 0 )->setPopup( menu, true );
00286     menu = new QPopupMenu( this );
00287     m_actions->action("printer_state_change")->plug( menu );
00288     m_actions->action("printer_spool_change")->plug( menu );
00289     menu->insertSeparator();
00290     m_actions->action("printer_hard_default")->plug( menu );
00291     m_actions->action("printer_soft_default")->plug( menu );
00292     m_actions->action("printer_remove")->plug( menu );
00293     menu->insertSeparator();
00294     m_actions->action("printer_configure")->plug( menu );
00295     m_actions->action("printer_test")->plug( menu );
00296     m_actions->action("printer_tool")->plug( menu );
00297     menu->insertSeparator();
00298     //m_menubar->insertItem( i18n( "Printer" ), menu );
00299     m_menubar->insertButton( "printer2", 1, true, i18n( "Printer" ) );
00300     m_menubar->getButton( 1 )->setPopup( menu, true );
00301     menu = new QPopupMenu( this );
00302     m_actions->action("server_restart")->plug( menu );
00303     m_actions->action("server_configure")->plug( menu );
00304     //m_menubar->insertItem( i18n( "Server" ), menu );
00305     m_menubar->insertButton( "misc", 2, true, i18n( "Print Server" ) );
00306     m_menubar->getButton( 2 )->setPopup( menu, true );
00307     menu = new QPopupMenu( this );
00308     m_actions->action("manager_configure")->plug( menu );
00309     m_actions->action("view_refresh")->plug( menu );
00310     //m_menubar->insertItem( i18n( "Manager" ), menu );
00311     m_menubar->insertButton( "konsole3", 3, true, i18n( "Print Manager" ) );
00312     m_menubar->getButton( 3 )->setPopup( menu, true );
00313     menu = new QPopupMenu( this );
00314     m_actions->action("view_printerinfos")->plug( menu );
00315     m_actions->action("view_change")->plug( menu );
00316     m_actions->action("orientation_change")->plug( menu );
00317     m_actions->action( "view_toolbar" )->plug ( menu );
00318     m_actions->action( "view_menubar" )->plug ( menu );
00319     menu->insertSeparator();
00320     m_actions->action("view_pfilter")->plug( menu );
00321     //m_menubar->insertItem( i18n( "View" ), menu );
00322     m_menubar->insertButton( "view_remove", 4, true, i18n( "View" ) );
00323     m_menubar->getButton( 4 )->setPopup( menu, true );
00324     //m_menubar->setMinimumHeight( m_menubar->heightForWidth( 1000 ) );
00325     menu = new QPopupMenu( this );
00326     m_actions->action( "invoke_help" )->plug( menu );
00327     m_actions->action( "invoke_web" )->plug( menu );
00328     m_menubar->insertButton( "help", 5, true, i18n( "Documentation" ) );
00329     m_menubar->getButton( 5 )->setPopup( menu, true );
00330 
00331     loadPluginActions();
00332     slotPrinterSelected(QString::null);
00333 }
00334 
00335 void KMMainView::slotRefresh()
00336 {
00337     KMTimer::self()->delay(10);
00338 }
00339 
00340 void KMMainView::slotTimer()
00341 {
00342     kdDebug() << "KMMainView::slotTimer" << endl;
00343     QPtrList<KMPrinter> *printerlist = m_manager->printerList();
00344     bool ok = m_manager->errorMsg().isEmpty();
00345     m_printerview->setPrinterList(printerlist);
00346     if (!ok && m_first)
00347     {
00348         showErrorMsg(i18n("An error occurred while retrieving the printer list."));
00349         m_first = false;
00350     }
00351 }
00352 
00353 void KMMainView::slotPrinterSelected(const QString& prname)
00354 {
00355     KMPrinter   *p = KMManager::self()->findPrinter(prname);
00356     m_current = p;
00357     if (p && !p->isSpecial())
00358         KMFactory::self()->manager()->completePrinter(p);
00359     m_printerpages->setPrinter(p);
00360 
00361     // update actions state (only if toolbar enabled, workaround for toolbar
00362     // problem).
00363     //if (m_toolbar->isEnabled())
00364     //{
00365         int     mask = (m_manager->hasManagement() ? m_manager->printerOperationMask() : 0);
00366         bool    sp = !(p && p->isSpecial());
00367         m_actions->action("printer_remove")->setEnabled(!sp || ((mask & KMManager::PrinterRemoval) && p && p->isLocal() && !p->isImplicit()));
00368         m_actions->action("printer_configure")->setEnabled(!sp || ((mask & KMManager::PrinterConfigure) && p && !p->isClass(true) /*&& p->isLocal()*/));
00369         m_actions->action("printer_hard_default")->setEnabled((sp && (mask & KMManager::PrinterDefault) && p && !p->isClass(true) && !p->isHardDefault() && p->isLocal()));
00370         m_actions->action("printer_soft_default")->setEnabled((sp && p && !p->isSoftDefault()));
00371         m_actions->action("printer_test")->setEnabled((sp && (mask & KMManager::PrinterTesting) && p && !p->isClass(true)));
00372         bool    stmask = (sp && (mask & KMManager::PrinterEnabling) && p);
00373         m_actions->action("printer_state_change")->setEnabled(stmask && p->isLocal());
00374         m_actions->action("printer_spool_change")->setEnabled(stmask);
00375         m_actions->action("printer_start")->setEnabled((stmask && p->state() == KMPrinter::Stopped));
00376         m_actions->action("printer_stop")->setEnabled((stmask && p->state() != KMPrinter::Stopped));
00377         m_actions->action("printer_enable")->setEnabled((stmask && !p->acceptJobs()));
00378         m_actions->action("printer_disable")->setEnabled((stmask && p->acceptJobs()));
00379 
00380         m_actions->action("printer_add")->setEnabled((mask & KMManager::PrinterCreation));
00381         mask = m_manager->serverOperationMask();
00382         m_actions->action("server_restart")->setEnabled((mask & KMManager::ServerRestarting));
00383         m_actions->action("server_configure")->setEnabled((mask & KMManager::ServerConfigure));
00384 
00385         KMFactory::self()->manager()->validatePluginActions(m_actions, p);
00386     //}
00387     m_actions->action("printer_tool")->setEnabled(p && !p->isClass(true) && !p->isRemote() && !p->isSpecial());
00388 }
00389 
00390 void KMMainView::setViewType(int ID)
00391 {
00392     ((KSelectAction*)m_actions->action("view_change"))->setCurrentItem(ID);
00393     slotChangeView(ID);
00394 }
00395 
00396 int KMMainView::viewType() const
00397 { return m_printerview->viewType(); }
00398 
00399 void KMMainView::slotChangeView(int ID)
00400 {
00401     kdDebug() << "KMMainView::slotChangeView" << endl;
00402     if (ID >= KMPrinterView::Icons && ID <= KMPrinterView::Tree)
00403         m_printerview->setViewType((KMPrinterView::ViewType)ID);
00404 }
00405 
00406 void KMMainView::slotRightButtonClicked(const QString& prname, const QPoint& p)
00407 {
00408     KMPrinter   *printer = KMManager::self()->findPrinter(prname);
00409     // construct popup menu
00410     m_pop->clear();
00411     if (printer)
00412     {
00413         m_current = printer;
00414         if (!printer->isSpecial())
00415         {
00416             if (printer->isLocal())
00417                 m_actions->action((printer->state() == KMPrinter::Stopped ? "printer_start" : "printer_stop"))->plug(m_pop);
00418             m_actions->action((printer->acceptJobs() ? "printer_disable" : "printer_enable"))->plug(m_pop);
00419             m_pop->insertSeparator();
00420         }
00421         if (!printer->isSoftDefault()) m_actions->action("printer_soft_default")->plug(m_pop);
00422         if (printer->isLocal() && !printer->isImplicit())
00423         {
00424             if (!printer->isHardDefault()) m_actions->action("printer_hard_default")->plug(m_pop);
00425             m_actions->action("printer_remove")->plug(m_pop);
00426             m_pop->insertSeparator();
00427             if (!printer->isClass(true))
00428             {
00429                 m_actions->action("printer_configure")->plug(m_pop);
00430                 m_actions->action("printer_test")->plug(m_pop);
00431                 m_actions->action("printer_tool")->plug(m_pop);
00432                 m_pop->insertSeparator();
00433             }
00434         }
00435         else
00436         {
00437             if (!printer->isClass(true))
00438             {
00439                 m_actions->action("printer_configure")->plug(m_pop);
00440                 m_actions->action("printer_test")->plug(m_pop);
00441             }
00442             m_pop->insertSeparator();
00443         }
00444         if (!printer->isSpecial())
00445         {
00446             QValueList<KAction*>    pactions = m_actions->actions("plugin");
00447             for (QValueList<KAction*>::Iterator it=pactions.begin(); it!=pactions.end(); ++it)
00448                 (*it)->plug(m_pop);
00449             if (pactions.count() > 0)
00450                 m_pop->insertSeparator();
00451         }
00452     }
00453     else
00454     {
00455         m_actions->action("printer_add")->plug(m_pop);
00456         m_actions->action("printer_add_special")->plug(m_pop);
00457         m_pop->insertSeparator();
00458         m_actions->action("server_restart")->plug(m_pop);
00459         m_actions->action("server_configure")->plug(m_pop);
00460         m_pop->insertSeparator();
00461         m_actions->action("manager_configure")->plug(m_pop);
00462         m_actions->action("view_refresh")->plug(m_pop);
00463         m_pop->insertSeparator();
00464     }
00465     m_actions->action("view_printerinfos")->plug(m_pop);
00466     m_actions->action("view_change")->plug(m_pop);
00467     m_actions->action("orientation_change")->plug(m_pop);
00468     m_actions->action("view_toolbar")->plug(m_pop);
00469     m_actions->action("view_menubar")->plug(m_pop);
00470     m_pop->insertSeparator();
00471     m_actions->action("view_pfilter")->plug(m_pop);
00472 
00473     // pop the menu
00474     m_pop->popup(p);
00475 }
00476 
00477 void KMMainView::slotChangePrinterState()
00478 {
00479     QString opname = sender()->name();
00480     if (m_current && opname.startsWith("printer_"))
00481     {
00482         opname = opname.mid(8);
00483         KMTimer::self()->hold();
00484         bool    result(false);
00485         if (opname == "enable")
00486             result = m_manager->enablePrinter(m_current, true);
00487         else if (opname == "disable")
00488             result = m_manager->enablePrinter(m_current, false);
00489         else if (opname == "start")
00490             result = m_manager->startPrinter(m_current, true);
00491         else if (opname == "stop")
00492             result = m_manager->startPrinter(m_current, false);
00493         if (!result)
00494             showErrorMsg(i18n("Unable to modify the state of printer %1.").arg(m_current->printerName()));
00495         KMTimer::self()->release(result);
00496     }
00497 }
00498 
00499 void KMMainView::slotRemove()
00500 {
00501     if (m_current)
00502     {
00503         KMTimer::self()->hold();
00504         bool    result(false);
00505         if (KMessageBox::warningYesNo(this,i18n("Do you really want to remove %1?").arg(m_current->printerName())) == KMessageBox::Yes)
00506             if (m_current->isSpecial())
00507             {
00508                 if (!(result=m_manager->removeSpecialPrinter(m_current)))
00509                     showErrorMsg(i18n("Unable to remove special printer %1.").arg(m_current->printerName()));
00510             }
00511             else if (!(result=m_manager->removePrinter(m_current)))
00512                 showErrorMsg(i18n("Unable to remove printer %1.").arg(m_current->printerName()));
00513         KMTimer::self()->release(result);
00514     }
00515 }
00516 
00517 void KMMainView::slotConfigure()
00518 {
00519     if (m_current)
00520     {
00521         KMTimer::self()->hold();
00522         bool    needRefresh(false);
00523         if (m_current->isSpecial())
00524         {
00525             KMSpecialPrinterDlg dlg(this);
00526             dlg.setPrinter(m_current);
00527             if (dlg.exec())
00528             {
00529                 KMPrinter   *prt = dlg.printer();
00530                 if (prt->name() != m_current->name())
00531                     m_manager->removeSpecialPrinter(m_current);
00532                 m_manager->createSpecialPrinter(prt);
00533                 needRefresh = true;
00534             }
00535         }
00536         else
00537         {
00538             DrMain  *driver = m_manager->loadPrinterDriver(m_current, true);
00539             if (driver)
00540             {
00541                 KMDriverDialog  dlg(this);
00542                 dlg.setCaption(i18n("Configure %1").arg(m_current->printerName()));
00543                 dlg.setDriver(driver);
00544                 // disable OK button for remote printer (read-only dialog)
00545                 if (m_current->isRemote())
00546                     dlg.enableButtonOK(false);
00547                 if (dlg.exec())
00548                     if (!m_manager->savePrinterDriver(m_current,driver))
00549                         showErrorMsg(i18n("Unable to modify settings of printer %1.").arg(m_current->printerName()));
00550                 delete driver;
00551             }
00552             else
00553                 showErrorMsg(i18n("Unable to load a valid driver for printer %1.").arg(m_current->printerName()));
00554         }
00555         KMTimer::self()->release(needRefresh);
00556     }
00557 }
00558 
00559 void KMMainView::slotAdd()
00560 {
00561     KMTimer::self()->hold();
00562 
00563     int result(0);
00564     if ((result=add_printer_wizard(this)) == -1)
00565         showErrorMsg(i18n("Unable to create printer."));
00566 
00567     KMTimer::self()->release((result == 1));
00568 }
00569 
00570 void KMMainView::slotHardDefault()
00571 {
00572     if (m_current)
00573     {
00574         KMTimer::self()->hold();
00575         bool    result = m_manager->setDefaultPrinter(m_current);
00576         if (!result)
00577             showErrorMsg(i18n("Unable to define printer %1 as default.").arg(m_current->printerName()));
00578         KMTimer::self()->release(result);
00579     }
00580 }
00581 
00582 void KMMainView::slotSoftDefault()
00583 {
00584     if (m_current)
00585     {
00586         KMTimer::self()->hold();
00587         KMFactory::self()->virtualManager()->setAsDefault(m_current,QString::null);
00588         KMTimer::self()->release(true);
00589     }
00590 }
00591 
00592 void KMMainView::setOrientation(int o)
00593 {
00594     int     ID = (o == Qt::Horizontal ? 1 : 0);
00595     ((KSelectAction*)m_actions->action("orientation_change"))->setCurrentItem(ID);
00596     slotChangeDirection(ID);
00597 }
00598 
00599 int KMMainView::orientation() const
00600 { return (m_boxlayout->direction() == QBoxLayout::LeftToRight ? Qt::Horizontal : Qt::Vertical);  }
00601 
00602 void KMMainView::slotChangeDirection(int d)
00603 {
00604     m_boxlayout->setDirection(d == 1 ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom);
00605 }
00606 
00607 void KMMainView::slotTest()
00608 {
00609     if (m_current)
00610     {
00611         KMTimer::self()->hold();
00612         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)
00613         {
00614             if (KMFactory::self()->manager()->testPrinter(m_current))
00615                 KMessageBox::information(this,i18n("Test page successfully sent to printer %1.").arg(m_current->printerName()));
00616             else
00617                 showErrorMsg(i18n("Unable to test printer %1.").arg(m_current->printerName()));
00618         }
00619         KMTimer::self()->release(true);
00620     }
00621 }
00622 
00623 void KMMainView::showErrorMsg(const QString& msg, bool usemgr)
00624 {
00625     QString s(msg);
00626     if (usemgr)
00627     {
00628         s.prepend("<p>");
00629         s.append(" ");
00630         s += i18n("Error message received from manager:</p><p>%1</p>");
00631         if (m_manager->errorMsg().isEmpty())
00632             s = s.arg(i18n("Internal error (no error message)."));
00633         else
00634             s = s.arg(m_manager->errorMsg());
00635         // clean up error message
00636         m_manager->setErrorMsg(QString::null);
00637     }
00638     s.prepend("<qt>").append("</qt>");
00639     KMTimer::self()->hold();
00640     KMessageBox::error(this,s);
00641     KMTimer::self()->release();
00642 }
00643 
00644 void KMMainView::slotServerRestart()
00645 {
00646     KMTimer::self()->hold();
00647     bool    result = m_manager->restartServer();
00648     if (!result)
00649     {
00650         showErrorMsg(i18n("Unable to restart print server."));
00651         KMTimer::self()->release( false );
00652     }
00653     else
00654     {
00655         m_printerview->setPrinterList( 0 );
00656         createMessageWindow( i18n( "Restarting server..." ), 0 );
00657         m_manager->checkUpdatePossible();
00658     }
00659 }
00660 
00661 void KMMainView::slotServerConfigure()
00662 {
00663     KMTimer::self()->hold();
00664     bool    result = m_manager->configureServer(this);
00665     if (!result)
00666     {
00667         showErrorMsg(i18n("Unable to configure print server."));
00668         KMTimer::self()->release( false );
00669     }
00670     else
00671     {
00672         m_printerview->setPrinterList( 0 );
00673         createMessageWindow( i18n( "Configuring server..." ), 0 );
00674         m_manager->checkUpdatePossible();
00675     }
00676 }
00677 
00678 void KMMainView::slotToggleToolBar(bool on)
00679 {
00680     if (on) m_toolbar->show();
00681     else m_toolbar->hide();
00682 }
00683 
00684 void KMMainView::slotToggleMenuBar( bool on )
00685 {
00686     if ( on )
00687         m_menubar->show();
00688     else
00689         m_menubar->hide();
00690 }
00691 
00692 void KMMainView::slotManagerConfigure()
00693 {
00694     KMTimer::self()->hold();
00695     KMConfigDialog  dlg(this,"ConfigDialog");
00696     bool    refresh(false);
00697     if ((refresh=dlg.exec()))
00698     {
00699         loadParameters();
00700     }
00701     KMTimer::self()->release(refresh);
00702 }
00703 
00704 void KMMainView::slotAddSpecial()
00705 {
00706     KMTimer::self()->hold();
00707     KMSpecialPrinterDlg dlg(this);
00708     if (dlg.exec())
00709     {
00710         KMPrinter   *prt = dlg.printer();
00711         m_manager->createSpecialPrinter(prt);
00712     }
00713     KMTimer::self()->release(true);
00714 }
00715 
00716 void KMMainView::slotShowPrinterInfos(bool on)
00717 {
00718     if (on)
00719         m_printerpages->show();
00720     else
00721         m_printerpages->hide();
00722     m_actions->action("orientation_change")->setEnabled(on);
00723 }
00724 
00725 void KMMainView::enableToolbar(bool on)
00726 {
00727     KToggleAction   *act = (KToggleAction*)m_actions->action("view_toolbar");
00728     m_toolbar->setEnabled(on);
00729     act->setEnabled(on);
00730     if (on && act->isChecked())
00731         m_toolbar->show();
00732     else
00733         m_toolbar->hide();
00734 }
00735 
00736 KAction* KMMainView::action(const char *name)
00737 {
00738     return m_actions->action(name);
00739 }
00740 
00741 /*
00742 void KMMainView::aboutToReload()
00743 {
00744     m_printerview->setPrinterList(0);
00745 }
00746 */
00747 
00748 void KMMainView::reload()
00749 {
00750     removePluginActions();
00751     loadPluginActions();
00752 
00753     // redo the connection as the old manager object has been removed
00754     connect( m_manager, SIGNAL( updatePossible( bool ) ), SLOT( slotUpdatePossible( bool ) ) );
00755 
00756     // We must delay the refresh such that all objects has been
00757     // correctly reloaded (otherwise, crash in KMJobViewer).
00758     KMTimer::self()->hold();
00759     m_printerview->setPrinterList( 0 );
00760     createMessageWindow( i18n( "Initializing manager..." ) );
00761     m_manager->checkUpdatePossible();
00762 }
00763 
00764 void KMMainView::showPrinterInfos(bool on)
00765 {
00766     static_cast<KToggleAction*>(m_actions->action("view_printerinfos"))->setChecked(on);
00767     slotShowPrinterInfos(on);
00768 }
00769 
00770 bool KMMainView::printerInfosShown() const
00771 {
00772     return (static_cast<KToggleAction*>(m_actions->action("view_printerinfos"))->isChecked());
00773 }
00774 
00775 void KMMainView::loadPluginActions()
00776 {
00777     KMFactory::self()->manager()->createPluginActions(m_actions);
00778     QValueList<KAction*>    pactions = m_actions->actions("plugin");
00779     int index = m_pactionsindex;
00780     //QPopupMenu *menu = m_menubar->findItem( m_menubar->idAt( 1 ) )->popup();
00781     QPopupMenu *menu = m_menubar->getButton( 1 )->popup();
00782     for (QValueList<KAction*>::Iterator it=pactions.begin(); it!=pactions.end(); ++it)
00783     {
00784         (*it)->plug(m_toolbar, index++);
00785         ( *it )->plug( menu );
00786     }
00787 }
00788 
00789 void KMMainView::removePluginActions()
00790 {
00791     QValueList<KAction*>    pactions = m_actions->actions("plugin");
00792     for (QValueList<KAction*>::Iterator it=pactions.begin(); it!=pactions.end(); ++it)
00793     {
00794         (*it)->unplugAll();
00795         delete (*it);
00796     }
00797 }
00798 
00799 void KMMainView::slotToolSelected(int ID)
00800 {
00801     KMTimer::self()->hold();
00802 
00803     QString libname = m_toollist[ID];
00804     libname.prepend("kdeprint_tool_");
00805     if (m_current && !m_current->device().isEmpty() && !libname.isEmpty())
00806     {
00807         KLibFactory *factory = KLibLoader::self()->factory(libname.local8Bit());
00808         if (factory)
00809         {
00810             QStringList args;
00811             args << m_current->device() << m_current->printerName();
00812             KDialogBase *dlg = static_cast<KDialogBase*>(factory->create(this, "Tool", 0, args));
00813             if (dlg)
00814                 dlg->exec();
00815             delete dlg;
00816         }
00817     }
00818     else
00819         KMessageBox::error(this,
00820             i18n("Unable to start printer tool. Possible reasons are: "
00821                  "no printer selected, the selected printer doesn't have "
00822                  "any local device defined (printer port), or the tool library "
00823                  "could not be found."));
00824 
00825     KMTimer::self()->release();
00826 }
00827 
00828 void KMMainView::slotToggleFilter(bool on)
00829 {
00830     KMTimer::self()->hold();
00831     KMManager::self()->enableFilter(on);
00832     KMTimer::self()->release(true);
00833 }
00834 
00835 void KMMainView::configChanged()
00836 {
00837     slotRefresh();
00838 }
00839 
00840 void KMMainView::slotUpdatePossible( bool flag )
00841 {
00842     destroyMessageWindow();
00843     if ( !flag )
00844         showErrorMsg( i18n( "Unable to retrieve the printer list." ) );
00845     KMTimer::self()->release( true );
00846 }
00847 
00848 void KMMainView::createMessageWindow( const QString& txt, int delay )
00849 {
00850     destroyMessageWindow();
00851     MessageWindow::add( m_printerview, txt, delay );
00852 }
00853 
00854 void KMMainView::destroyMessageWindow()
00855 {
00856     MessageWindow::remove( m_printerview );
00857 }
00858 
00859 void KMMainView::slotInit()
00860 {
00861     KMTimer::self()->hold();
00862     m_printerview->setPrinterList( 0 );
00863     createMessageWindow( i18n( "Initializing manager..." ) );
00864     m_manager->checkUpdatePossible();
00865 }
00866 
00867 void KMMainView::slotHelp()
00868 {
00869     QString s = sender()->name();
00870     if ( s == "invoke_help" )
00871         kapp->invokeHelp( QString::null, "kdeprint" );
00872     else if ( s == "invoke_web" )
00873     {
00874         QStringList args;
00875         args << "exec" << "http://printing.kde.org";
00876         kapp->kdeinitExec( "kfmclient", args );
00877     }
00878     else
00879         kdDebug( 500 ) << "Unknown help invokator: " << s << endl;
00880 }
00881 
00882 #include "kmmainview.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:36:23 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003