Back to index

scribus-ng  1.3.4.dfsg+svn20071115
helpbrowser.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008 *   Copyright (C) 2004 by Craig Bradney                                   *
00009 *   cbradney@zip.com.au                                                   *
00010 *   Copyright (C) 2005 by Petr Vanek                                      *
00011 *   petr@yarpen.cz                                                        *
00012 *                                                                         *
00013 *   This program is free software; you can redistribute it and/or modify  *
00014 *   it under the terms of the GNU General Public License as published by  *
00015 *   the Free Software Foundation; either version 2 of the License, or     *
00016 *   (at your option) any later version.                                   *
00017 *                                                                         *
00018 *   This program is distributed in the hope that it will be useful,       *
00019 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00020 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00021 *   GNU General Public License for more details.                          *
00022 *                                                                         *
00023 *   You should have received a copy of the GNU General Public License     *
00024 *   along with this program; if not, write to the                         *
00025 *   Free Software Foundation, Inc.,                                       *
00026 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00027 ***************************************************************************/
00028 
00029 #include "helpbrowser.h"
00030 #include "helpbrowser.moc"
00031 
00032 #include <qvariant.h>
00033 #include <qstring.h>
00034 #include <qpushbutton.h>
00035 #include <qtabwidget.h>
00036 #include <qwidget.h>
00037 #include <qheader.h>
00038 #include <qtextbrowser.h>
00039 #include <qlayout.h>
00040 #include <qtooltip.h>
00041 #include <qtoolbutton.h>
00042 #include <qwhatsthis.h>
00043 #include <qimage.h>
00044 #include <qfileinfo.h>
00045 #include <qfiledialog.h>
00046 #include <qtextcodec.h>
00047 #include <qdom.h>
00048 #include <qdir.h>
00049 #include <qsplitter.h>
00050 #include <qmime.h>
00051 #include <qlabel.h>
00052 #include <qlineedit.h>
00053 #include <qmessagebox.h>
00054 #include <qaccel.h>
00055 #include <qinputdialog.h>
00056 #include <qmenubar.h>
00057 #include <qpixmap.h>
00058 #include <qprocess.h>
00059 #include <qpopupmenu.h>
00060 #include <qprinter.h>
00061 #include <qpainter.h>
00062 #include <qpaintdevicemetrics.h>
00063 #include <qsimplerichtext.h>
00064 #include <qxml.h>
00065 #include <qcursor.h>
00066 
00067 #include "scpaths.h"
00068 #include "util.h"
00069 #include "scconfig.h"
00070 #include "scpaths.h"
00071 #include "prefsmanager.h"
00072 #include "prefsfile.h"
00073 
00074 #if defined(_WIN32)
00075 #include <windows.h>
00076 #include <shellapi.h>
00077 #endif
00078 
00079 extern QPixmap loadIcon(QString nam);
00080 
00081 
00086 QString bookmarkFile()
00087 {
00088        QString appDataDir(ScPaths::getApplicationDataDir());
00089        QString fname(appDataDir + "doc/bookmarks.xml");
00090        if (!QFile::exists(fname))
00091        {
00092               QDir d(QDir::convertSeparators(appDataDir));
00093               d.mkdir("doc");
00094        }
00095        return fname;
00096 }
00097 
00098 
00103 QString historyFile()
00104 {
00105        QString appDataDir(ScPaths::getApplicationDataDir());
00106        QString fname(appDataDir + "doc/history.xml");
00107        if (!QFile::exists(fname))
00108        {
00109               QDir d(QDir::convertSeparators(appDataDir));
00110               d.mkdir("doc");
00111        }
00112        return fname;
00113 }
00114 
00115 
00122 class BookmarkParser : public QXmlDefaultHandler
00123 {
00124        public:
00125               QListView* view;
00126 
00127               bool startDocument()
00128               {
00129                      return true;
00130               }
00131 
00132               bool startElement(const QString&, const QString&, const QString& qName, const QXmlAttributes& attrs)
00133               {
00134                      if (qName == "item")
00135                      {
00136                             QListViewItem *item = new QListViewItem(view, attrs.value(0), attrs.value(1));
00137                             view->insertItem(item);
00138                      }
00139                      return true;
00140               }
00141 
00142               bool endElement(const QString&, const QString&, const QString&)
00143               {
00144                      return true;
00145               }
00146 };
00147 
00154 class HistoryParser : public QXmlDefaultHandler
00155 {
00156        public:
00157               HelpBrowser *helpBrowser;
00158 
00159               bool startDocument()
00160               {
00161                      return true;
00162               }
00163 
00164               bool startElement(const QString&, const QString&, const QString& qName, const QXmlAttributes& attrs)
00165               {
00166                      if (qName == "item")
00167                      {
00168                             struct histd his;
00169                             his.title = attrs.value(0);
00170                             his.url = attrs.value(1);
00171                             helpBrowser->mHistory[helpBrowser->histMenu->insertItem(his.title)] = his;
00172                      }
00173                      return true;
00174               }
00175 
00176               bool endElement(const QString&, const QString&, const QString&)
00177               {
00178                      return true;
00179               }
00180 };
00181 
00182 int HelpListItem::compare(QListViewItem *i, int col, bool asc) const
00183 {
00184        if (col == 1)
00185        {
00186               int d = text(col).toInt() - i->text(col).toInt();
00187               if (d > 0)
00188                      return 1;
00189               return -1;
00190        }
00191        else
00192               return QListViewItem::compare(i, col, asc);
00193 }
00194 
00195 TextBrowser::TextBrowser(QWidget * parent, const char * name) : QTextBrowser(parent, name)
00196 {
00197        connect(this, SIGNAL(highlighted(const QString &)), this, SLOT(overLink(const QString &)));
00198 }
00199 
00200 void TextBrowser::setSource(const QString &name)
00201 {
00202 #if defined(_WIN32)
00203        int index = name.find( "http:/" );
00204        if ( index >=0 )
00205        {
00206               QString url = name.right(name.length() - index);
00207               //textBrowser->setSource( textBrowser->source() );
00208               QT_WA( {
00209               ShellExecute( winId(), 0, (TCHAR*)url.ucs2(), 0, 0, SW_SHOWNORMAL );
00210            } , {
00211               ShellExecuteA( winId(), 0, url.local8Bit(), 0, 0, SW_SHOWNORMAL );
00212            } );
00213               return;
00214        }
00215 #endif
00216 #if !defined(QT_MAC) && !defined(_WIN32)
00217        if (name.left(7)=="http://")
00218        {
00219               QString extBrowser=PrefsManager::instance()->extBrowserExecutable();
00220               QFileInfo fi(extBrowser);
00221               if (extBrowser.isEmpty() || !fi.exists())
00222               {
00223                      extBrowser = QFileDialog::getOpenFileName(QString::null, QString::null, this, "changeExtBrowser", tr("Locate your web browser"));
00224                      if (!QFileInfo(extBrowser).exists())
00225                             extBrowser="";
00226                      PrefsManager::instance()->setExtBrowserExecutable(extBrowser);
00227               }             
00228               if (!extBrowser.isEmpty())
00229               {
00230                      QStringList args;
00231                      args.append(extBrowser);
00232                      args.append(name);
00233                      QProcess webProc(args);
00234                      if (!webProc.start())
00235                             QMessageBox::critical(this, tr("External Web Browser Failed to Start"), tr("Scribus was not able to start the external web browser application %1. Please check the setting in Preferences").arg(PrefsManager::instance()->extBrowserExecutable()), QMessageBox::Ok, QMessageBox::NoButton);
00236               }
00237        }
00238        else
00239               QTextBrowser::setSource(name);
00240 #else
00241        QTextBrowser::setSource(name);
00242 #endif
00243 }
00244 
00245 void TextBrowser::overLink(const QString &link)
00246 {
00247        if ((link.isNull()) || (link.isEmpty()))
00248               qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00249        else
00250               qApp->setOverrideCursor(QCursor(PointingHandCursor), true);
00251 }
00252 
00253 HelpBrowser::HelpBrowser( QWidget* parent, QString /*caption*/, QString guiLanguage, QString jumpToSection, QString jumpToFile)
00254        : QWidget( parent, "Help", WType_TopLevel | WDestructiveClose | WGroupLeader )
00255 {
00256        QString fileName;
00257        mHistory.clear();
00258        struct histd his;
00259        language = guiLanguage.isEmpty() ? QString("en") : guiLanguage.left(2);
00260        helpBrowsermainLayout = new QVBoxLayout( this);
00261        buttonLayout = new QHBoxLayout;
00262        buttonLayout->setSpacing( 6 );
00263        buttonLayout->setMargin( 2 );
00264 
00265        homeButton = new QToolButton( this, "homeButton" );
00266        homeButton->setText( "" );
00267        homeButton->setPixmap(loadIcon("16/go-home.png"));
00268        homeButton->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed, homeButton->sizePolicy().hasHeightForWidth() ) );
00269        buttonLayout->addWidget( homeButton );
00270 
00271        histMenu = new QPopupMenu( this );
00272        backButton = new QToolButton( this, "backButton" );
00273        backButton->setPixmap(loadIcon("16/go-previous.png"));
00274        backButton->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed, backButton->sizePolicy().hasHeightForWidth() ) );
00275        backButton->setPopup(histMenu);
00276        backButton->setPopupDelay(0);
00277        buttonLayout->addWidget( backButton );
00278 
00279        forwButton = new QToolButton( this, "forwButton" );
00280        forwButton->setPixmap(loadIcon("16/go-next.png"));
00281        forwButton->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed, forwButton->sizePolicy().hasHeightForWidth() ) );
00282        buttonLayout->addWidget( forwButton );
00283        QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00284        buttonLayout->addItem( spacer );
00285        helpBrowsermainLayout->addLayout( buttonLayout );
00286 
00287        helpBrowserLayout = new QHBoxLayout;
00288        splitter = new QSplitter(this, "splitter");
00289        splitter->setChildrenCollapsible( false );
00290        helpBrowserLayout->addWidget( splitter );
00291 
00292        tabWidget = new QTabWidget( splitter, "tabWidget" );
00293        tabContents = new QWidget( tabWidget, "tabContents" );
00294        tabLayout = new QHBoxLayout( tabContents, 11, 6, "tabLayout");
00295 
00296        listView = new QListView( tabContents, "listView" );
00297        listView->addColumn( tr( "Contents" ) );
00298        listView->addColumn( tr( "Link" ) , 0 );
00299        listView->setColumnWidthMode( 0, QListView::Maximum );
00300        listView->setColumnWidthMode( 1, QListView::Manual );
00301        listView->setSorting(-1,true);
00302        listView->setRootIsDecorated( true );
00303        listView->setSelectionMode(QListView::Single);
00304        listView->setDefaultRenameAction(QListView::Reject);
00305        listView->clear();
00306        tabLayout->addWidget( listView );
00307 
00308        tabWidget->insertTab( tabContents, tr("&Contents") );
00309 
00310        // searching
00311        tabSearching = new QWidget(tabWidget, "tabSearching");
00312        searchingMainLayout = new QVBoxLayout(tabSearching, 11, 6, "searchingMainLayout");
00313        searchingButtonLayout = new QHBoxLayout;
00314        searchingEdit = new QLineEdit(tabSearching, "searchingEdit");
00315        searchingButton = new QPushButton( tr("&Search"), tabSearching, "searchingButton");
00316        searchingButtonLayout->addWidget(searchingEdit);
00317        searchingButtonLayout->addWidget(searchingButton);
00318        searchingMainLayout->addLayout(searchingButtonLayout);
00319        searchingView = new QListView(tabSearching, "searchingView");
00320        searchingView->addColumn( tr("Contents"));
00321        searchingView->addColumn( tr("Relevance"));
00322        searchingView->addColumn( tr("Link" ) ,0);
00323        searchingView->setColumnWidthMode( 0, QListView::Maximum );
00324        searchingView->setColumnWidthMode( 1, QListView::Manual );
00325        searchingView->setSorting(1, false);
00326        searchingView->setRootIsDecorated( true );
00327        searchingView->setSelectionMode(QListView::Single);
00328        searchingView->setDefaultRenameAction(QListView::Reject);
00329        searchingView->setAllColumnsShowFocus(true);
00330        searchingView->clear();
00331        searchingMainLayout->addWidget(searchingView);
00332        tabWidget->insertTab(tabSearching, tr("Se&arch"));
00333 
00334        // bookmarks
00335        tabBookmarks = new QWidget(tabWidget, "tabBookmarks");
00336        bookmarksMainLayout = new QVBoxLayout(tabBookmarks, 11, 6, "bookmarksMainLayout");
00337        bookmarksView = new QListView(tabBookmarks, "bookmarksView");
00338        bookmarksView->addColumn( tr( "Contents" ) );
00339        bookmarksView->addColumn( tr( "Link" ) , 0 );
00340        bookmarksView->setColumnWidthMode( 0, QListView::Maximum );
00341        bookmarksView->setColumnWidthMode( 1, QListView::Manual );
00342        bookmarksView->setSorting(1, true);
00343        bookmarksView->setRootIsDecorated( true );
00344        bookmarksView->setSelectionMode(QListView::Single);
00345        bookmarksView->setDefaultRenameAction(QListView::Reject);
00346        bookmarksView->clear();
00347        bookmarksMainLayout->addWidget(bookmarksView);
00348        bookmarksButtonLayout = new QHBoxLayout;
00349        bookmarkButton = new QPushButton( tr("&New"), tabBookmarks, "bookmarkButton");
00350        deleteBookmarkButton = new QPushButton( tr("&Delete"), tabBookmarks, "deleteBookmarkButton");
00351        deleteAllBookmarkButton = new QPushButton( tr("De&lete All"), tabBookmarks, "deleteAllBookmarkButton");
00352        bookmarksButtonLayout->addWidget(bookmarkButton);
00353        bookmarksButtonLayout->addWidget(deleteBookmarkButton);
00354        bookmarksButtonLayout->addWidget(deleteAllBookmarkButton);
00355        bookmarksMainLayout->addLayout(bookmarksButtonLayout);
00356        tabWidget->insertTab(tabBookmarks, tr("Book&marks"));
00357 
00358        textBrowser = new TextBrowser( splitter, "textBrowser" );
00359        textBrowser->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum, false ) );
00360        textBrowser->setFrameShape( QTextBrowser::StyledPanel );
00361        QMimeSourceFactory *textBrowserMSF=textBrowser->mimeSourceFactory();
00362        textBrowserMSF->setExtensionType("html", "text/html;charset=UTF-8");
00363        helpBrowsermainLayout->addLayout( helpBrowserLayout );
00364 
00365        // menus
00366        menuBar = new QMenuBar(this);
00367        QPopupMenu *fileMenu = new QPopupMenu(this);
00368        fileMenu->insertItem(loadIcon("16/document-print.png"), tr("&Print..."), this, SLOT(print()), CTRL+Key_P);
00369        fileMenu->insertSeparator();
00370        fileMenu->insertItem(loadIcon("exit.png"), tr("E&xit"), this, SLOT(close()));
00371        menuBar->insertItem( tr("&File"), fileMenu);
00372        QPopupMenu *editMenu = new QPopupMenu(this);
00373        editMenu->insertItem(loadIcon("find.png"), tr("&Find..."), this, SLOT(find()), CTRL+Key_F);
00374        editMenu->insertItem( tr("Find &Next"), this, SLOT(findNext()), Key_F3);
00375        editMenu->insertItem( tr("Find &Previous"), this, SLOT(findPrevious()), SHIFT+Key_F3);
00376        menuBar->insertItem( tr("&Edit"), editMenu);
00377        QPopupMenu *bookmarkMenu = new QPopupMenu(this);
00378        bookmarkMenu->insertItem( tr("&Add Bookmark"), this, SLOT(bookmarkButton_clicked()), CTRL+Key_D);
00379        bookmarkMenu->insertItem( tr("&Delete"), this, SLOT(deleteBookmarkButton_clicked()));
00380        bookmarkMenu->insertItem( tr("D&elete All"), this, SLOT(deleteAllBookmarkButton_clicked()));
00381        helpBrowsermainLayout->setMenuBar(menuBar);
00382        menuBar->insertItem( tr("&Bookmarks"), bookmarkMenu);
00383        languageChange();
00384        loadMenu();
00385        listView->header()->hide();
00386        searchingView->header()->hide();
00387        bookmarksView->header()->hide();
00388        jumpToHelpSection(jumpToSection, jumpToFile );
00389        readBookmarks();
00390        readHistory();
00391        splitter->setResizeMode( tabWidget, QSplitter::KeepSize );
00392        splitter->setResizeMode( textBrowser, QSplitter::Stretch );
00393        // reset previous size
00394        prefs = PrefsManager::instance()->prefsFile->getPluginContext("helpbrowser");
00395        int xsize = prefs->getUInt("xsize", 640);
00396        int ysize = prefs->getUInt("ysize", 480);
00397        resize(QSize(xsize, ysize).expandedTo(minimumSizeHint()) );
00398        clearWState( WState_Polished );
00399 
00400        connect( homeButton, SIGNAL( clicked() ), textBrowser, SLOT( home() ) );
00401        connect( forwButton, SIGNAL( clicked() ), textBrowser, SLOT( forward() ) );
00402        connect( backButton, SIGNAL( clicked() ), textBrowser, SLOT( backward() ) );
00403        connect( histMenu, SIGNAL(activated(int)), this, SLOT(histChosen(int)));
00404        connect( listView, SIGNAL(clicked( QListViewItem *)), this, SLOT(itemSelected( QListViewItem *)));
00405        // searching
00406        connect(searchingEdit, SIGNAL(returnPressed()), this, SLOT(searchingButton_clicked()));
00407        connect(searchingView, SIGNAL(clicked( QListViewItem *)), this, SLOT(itemSearchSelected(QListViewItem *)));
00408        connect(searchingButton, SIGNAL(clicked()), this, SLOT(searchingButton_clicked()));
00409        // bookmarks
00410        connect(bookmarkButton, SIGNAL(clicked()), this, SLOT(bookmarkButton_clicked()));
00411        connect(deleteBookmarkButton, SIGNAL(clicked()), this, SLOT(deleteBookmarkButton_clicked()));
00412        connect(deleteAllBookmarkButton, SIGNAL(clicked()), this, SLOT(deleteAllBookmarkButton_clicked()));
00413        connect(bookmarksView, SIGNAL(clicked(QListViewItem *)), this, SLOT(itemBookmarkSelected(QListViewItem *)));
00414 }
00415 
00416 HelpBrowser::~HelpBrowser()
00417 {
00418        // no need to delete child widgets, Qt does it all for us
00419        // bookmarks
00420        QFile bookFile(bookmarkFile());
00421        if (bookFile.open(IO_WriteOnly))
00422        {
00423               QTextStream stream(&bookFile);
00424               stream.setEncoding(QTextStream::UnicodeUTF8);
00425               stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00426               stream << "<bookmarks>\n";
00427               QListViewItemIterator it(bookmarksView);
00428               for ( ; it.current(); ++it)
00429                      stream << "\t<item title=\"" << it.current()->text(0) << "\" url=\"" << it.current()->text(1) << "\" />\n";
00430               stream << "</bookmarks>\n";
00431               bookFile.close();
00432        }
00433        // history
00434        QFile histFile(historyFile());
00435        if (histFile.open(IO_WriteOnly))
00436        {
00437               QTextStream stream(&histFile);
00438               stream.setEncoding(QTextStream::UnicodeUTF8);
00439               stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00440               stream << "<history>\n";
00441               for (QMap<int,histd>::Iterator it = mHistory.begin() ; it != mHistory.end(); ++it)
00442                      stream << "\t<item title=\"" << it.data().title << "\" url=\"" << it.data().url << "\" />\n";
00443               stream << "</history>\n";
00444               histFile.close();
00445        }
00446        // size
00447        prefs->set("xsize", width());
00448        prefs->set("ysize", height());
00449 }
00450 
00451 void HelpBrowser::languageChange()
00452 {
00453        setCaption( tr( "Scribus Online Help" ) );
00454        listView->header()->setLabel( 0, tr( "Contents" ) );
00455        listView->clear();
00456 
00457        tabWidget->changeTab( tabContents, tr( "&Contents" ) );
00458        QToolTip::add(searchingEdit, tr("Searching is case unsensitive"));
00459 }
00460 
00461 void HelpBrowser::histChosen(int i)
00462 {
00463        if (mHistory.contains(i))
00464               textBrowser->setSource(mHistory[i].url);
00465 }
00466 
00467 void HelpBrowser::jumpToHelpSection(QString jumpToSection, QString jumpToFile)
00468 {
00469        QString toLoad;
00470        bool noDocs=false;
00471 
00472        if (jumpToFile.isEmpty())
00473        {
00474               toLoad = ScPaths::instance().docDir() + language + "/"; //clean this later to handle 5 char locales
00475               if (jumpToSection.isEmpty())
00476               {
00477                      if (listView->firstChild())
00478                      {
00479                             toLoad+=listView->firstChild()->text(1);
00480                             listView->setSelected( listView->firstChild(), true );
00481                      }
00482                      else
00483                             noDocs=true;
00484               }
00485               else if (jumpToSection=="scripter")
00486               {
00487                      toLoad+="scripter1.html";
00488                      QListViewItemIterator it(listView);
00489                      while (it.current())
00490                      {
00491                             if (it.current()->text(1)=="scripter1.html")
00492                                    listView->setSelected( it.current(), true );
00493                             if (it.current()->text(1)=="developers.html")
00494                                    it.current()->setOpen( true );
00495                             ++it;
00496                      }
00497               }
00498        }
00499        else
00500               toLoad=jumpToFile;
00501 
00502        if (!noDocs)
00503               loadHelp(toLoad);
00504        else
00505               textBrowser->setText("<h2>"+ tr("Sorry, no manual available! Please see: http://docs.scribus.net for updated docs\nand www.scribus.net for downloads.")+"</h2>");
00506 }
00507 
00508 void HelpBrowser::loadHelp(QString filename)
00509 {
00510        struct histd his;
00511        bool Avail = true;
00512        QString toLoad;
00513        QFileInfo fi;
00514        fi = QFileInfo(filename);
00515        if (fi.fileName().length()>0)
00516        {
00517               if (fi.exists())
00518                      toLoad=filename;
00519               else
00520               {
00521                      toLoad = QDir::convertSeparators(ScPaths::instance().docDir() + "en/index.html");
00522                      language="en";
00523                      fi = QFileInfo(toLoad);
00524                      if (!fi.exists())
00525                      {
00526                             textBrowser->setText("<h2>"+ tr("Sorry, no manual available! Please see: http://docs.scribus.net for updated docs\nand www.scribus.net for downloads.")+"</h2>");
00527                             Avail = false;
00528                      }
00529               }
00530        }
00531        else
00532               Avail=false;
00533        if (Avail)
00534        {
00535               textBrowser->setSource(toLoad);
00536               his.title = textBrowser->documentTitle();
00537               if (his.title.isEmpty())
00538                      his.title = toLoad;
00539               his.url = toLoad;
00540               mHistory[histMenu->insertItem(his.title)] = his;
00541        }
00542        if (mHistory.count() > 15)
00543        {
00544               int itk = histMenu->idAt(0);
00545               mHistory.remove(itk);
00546               histMenu->removeItem(itk);
00547        }
00548 }
00549 
00550 void HelpBrowser::loadMenu()
00551 {
00552        QString pfad = ScPaths::instance().docDir();
00553        QString toLoad;
00554        QString pfad2 = QDir::convertSeparators(pfad + language + "/menu.xml");
00555        QFileInfo fi = QFileInfo(pfad2);
00556 
00557        if (fi.exists())
00558               toLoad=pfad2;
00559        else
00560        {
00561               toLoad = QDir::convertSeparators(pfad + "en/menu.xml");
00562               language="en";
00563               fi = QFileInfo(toLoad);
00564               sDebug("Scribus help in your selected language does not exist, trying English. Otherwise, please visit http://docs.scribus.net.");
00565        }
00566 
00567        if (fi.exists())
00568        {
00569               QDomDocument doc( "menuentries" );
00570               QFile file( toLoad );
00571               if ( !file.open( IO_ReadOnly ) )
00572                      return;
00573               if ( !doc.setContent( &file ) )
00574               {
00575                      file.close();
00576                      return;
00577               }
00578               file.close();
00579 
00580               QDomElement docElem = doc.documentElement();
00581               QDomNode n = docElem.firstChild();
00582               QListViewItem *qlvi=NULL, *qlvi2=NULL, *qlvi3=NULL, *qlvi4=NULL, *qlvi5=NULL, *qlvi6=NULL;
00583               QListViewItem *tutorialsMenuItem=NULL;
00584               bool haveTutorials=false;
00585 
00586               while( !n.isNull() )
00587               {
00588                      QDomElement e = n.toElement(); // try to convert the node to an element.
00589                      if( !e.isNull() )
00590                      {
00591                             if (e.hasAttribute( "text" ) && e.hasAttribute( "file" ))
00592                             {
00593                                    QDomAttr textAttr = e.attributeNode( "text" );
00594                                    QDomAttr fileAttr = e.attributeNode( "file" );
00595                                    if (qlvi2==NULL)
00596                                           qlvi=new QListViewItem(listView, textAttr.value(), fileAttr.value());
00597                                    else
00598                                           qlvi=new QListViewItem(listView, qlvi2, textAttr.value(), fileAttr.value());
00599                                    if (qlvi!=NULL && e.hasAttribute( "section" ))
00600                                    {
00601                                           QDomAttr sectionAttr = e.attributeNode( "section" );
00602                                           if (sectionAttr.value()=="tutorials" && !haveTutorials)
00603                                           {
00604                                                  haveTutorials=true;
00605                                                  tutorialsMenuItem=qlvi;
00606                                           }
00607                                    }
00608 
00609                                    if (qlvi!=NULL)
00610                                           qlvi2=qlvi;
00611                             }
00612 
00613                             QDomNodeList nl=n.childNodes();
00614                             for(uint i=0 ; i<= nl.count() ; i++)
00615                             {
00616                                    QDomNode child=nl.item(i);
00617                                    if (child.isElement())
00618                                    {
00619                                           QDomElement ec = child.toElement();
00620                                           if (!ec.isNull())
00621                                           {
00622                                                  if (ec.hasAttribute( "text" ) && ec.hasAttribute( "file" ))
00623                                                  {
00624                                                         QDomAttr textAttr = ec.attributeNode( "text" );
00625                                                         QDomAttr fileAttr = ec.attributeNode( "file" );
00626                                                         if (qlvi4==NULL)
00627                                                                qlvi3=new QListViewItem(qlvi, textAttr.value(), fileAttr.value());
00628                                                         else
00629                                                                qlvi3=new QListViewItem(qlvi, qlvi4, textAttr.value(), fileAttr.value());
00630                                                         if (qlvi3!=NULL && ec.hasAttribute( "section" ))
00631                                                         {
00632                                                                QDomAttr sectionAttr = e.attributeNode( "section" );
00633                                                                if (sectionAttr.value()=="tutorials" && !haveTutorials)
00634                                                                {
00635                                                                       haveTutorials=true;
00636                                                                       tutorialsMenuItem=qlvi3;
00637                                                                }
00638                                                         }
00639                                                         if (qlvi3!=NULL)
00640                                                                qlvi4=qlvi3;
00641                                                  }
00642                                                  //3rd level
00643                                                  QDomNodeList nl2=child.childNodes();
00644                                                  for(uint i=0 ; i<= nl2.count() ; i++)
00645                                                  {
00646                                                         QDomNode childchild=nl2.item(i);
00647                                                         if (childchild.isElement())
00648                                                         {
00649                                                                QDomElement ecc = childchild.toElement();
00650                                                                if (!ecc.isNull())
00651                                                                {
00652                                                                       if (ecc.hasAttribute( "text" ) && ecc.hasAttribute( "file" ))
00653                                                                       {
00654                                                                              QDomAttr textAttr = ecc.attributeNode( "text" );
00655                                                                              QDomAttr fileAttr = ecc.attributeNode( "file" );
00656                                                                              if (qlvi6==NULL)
00657                                                                                     qlvi5=new QListViewItem(qlvi3, textAttr.value(), fileAttr.value());
00658                                                                              else
00659                                                                                     qlvi5=new QListViewItem(qlvi3, qlvi6, textAttr.value(), fileAttr.value());
00660                                                                              if (qlvi5!=NULL && ecc.hasAttribute( "section" ))
00661                                                                              {
00662                                                                                     QDomAttr sectionAttr = e.attributeNode( "section" );
00663                                                                                     if (sectionAttr.value()=="tutorials" && !haveTutorials)
00664                                                                                     {
00665                                                                                            haveTutorials=true;
00666                                                                                            tutorialsMenuItem=qlvi5;
00667                                                                                     }
00668                                                                              }
00669                                                                       }
00670                                                                       if (qlvi5!=NULL)
00671                                                                              qlvi6=qlvi5;
00672                                                                }
00673                                                         }
00674                                                  }
00675                                           }
00676                                    }
00677                             }
00678                      }
00679                      n = n.nextSibling();
00680               }
00681               //scan for installed tutorials
00682               if (haveTutorials)
00683               {
00684                      QString path = ScPaths::instance().docDir();
00685                      path += language + "/tutorials/";
00686                      QDir dir(path, "*", QDir::Name, QDir::Dirs | QDir::NoSymLinks);
00687 
00688                      if (dir.exists() && (dir.count() != 0))
00689                      {
00690                             for (uint i = 0; i < dir.count(); ++i)
00691                             {
00692                                    if (dir[i]!="." && dir[i]!="..")
00693                                    {
00694                                           QString tutorialdir = QDir::convertSeparators("tutorials/" + dir[i] + "/");
00695                                           QFileInfo file(QDir::convertSeparators(path + dir[i] + "/menu.xml"));
00696                                           if (file.exists())  // menu.xml exists for tutorial
00697                                           {
00698                                                  QDomDocument docTutorial( "tutorialmenuentries" );
00699                                                  QFile fileTutorialMenu( file.filePath() );
00700                                                  if ( !fileTutorialMenu.open( IO_ReadOnly ) )
00701                                                         break;
00702                                                  if ( !docTutorial.setContent( &fileTutorialMenu ) )
00703                                                  {
00704                                                         fileTutorialMenu.close();
00705                                                         break;
00706                                                  }
00707                                                  fileTutorialMenu.close();
00708 
00709                                                  QDomElement docElemTutorial = docTutorial.documentElement();
00710                                                  QDomNode nTutorial = docElemTutorial.firstChild();
00711                                                  QListViewItem *tutorialQLVI=NULL;
00712 
00713                                                  while( !nTutorial.isNull() )
00714                                                  {
00715                                                         QDomElement eTutorial = nTutorial.toElement(); // try to convert the node to an element.
00716                                                         if( !eTutorial.isNull() )
00717                                                         {
00718                                                                if (tutorialsMenuItem!=NULL && eTutorial.hasAttribute( "text" ) && eTutorial.hasAttribute( "file" ))
00719                                                                {
00720                                                                       QDomAttr textAttr = eTutorial.attributeNode( "text" );
00721                                                                       QDomAttr fileAttr = eTutorial.attributeNode( "file" );
00722                                                                       tutorialQLVI=new QListViewItem(tutorialsMenuItem, textAttr.value(), tutorialdir + fileAttr.value());
00723                                                                }
00724                                                                QDomNodeList nl=nTutorial.childNodes();
00725                                                                QListViewItem *tutorialSubMenuItem, *tutorialSubMenuItemLast=NULL;
00726                                                                for(uint j=0 ; j<= nl.count() ; j++)
00727                                                                {
00728                                                                       QDomNode child=nl.item(j);
00729                                                                       if (child.isElement())
00730                                                                       {
00731                                                                              QDomElement ec = child.toElement();
00732                                                                              if (!ec.isNull())
00733                                                                              {
00734                                                                                     if (ec.hasAttribute( "text" ) && ec.hasAttribute( "file" ))
00735                                                                                     {
00736                                                                                            QDomAttr textAttr = ec.attributeNode( "text" );
00737                                                                                            QDomAttr fileAttr = ec.attributeNode( "file" );
00738                                                                                            if (tutorialSubMenuItemLast==NULL)
00739                                                                                                   tutorialSubMenuItem=new QListViewItem(tutorialQLVI, textAttr.value(), tutorialdir + fileAttr.value());
00740                                                                                            else
00741                                                                                                   tutorialSubMenuItem=new QListViewItem(tutorialQLVI, tutorialSubMenuItemLast, textAttr.value(), tutorialdir + fileAttr.value());
00742                                                                                            if (tutorialSubMenuItem!=NULL)
00743                                                                                                   tutorialSubMenuItemLast=tutorialSubMenuItem;
00744                                                                                     }
00745                                                                              }
00746                                                                       }
00747                                                                }
00748                                                         }
00749                                                         nTutorial = nTutorial.nextSibling();
00750                                                  }
00751                                           }
00752                                    }
00753                             }
00754                      }
00755 
00756               }
00757        }
00758 }
00759 
00760 void HelpBrowser::itemSelected(QListViewItem *item)
00761 {
00762        if ( !item )
00763               return;
00764        if (!item->text(1).isNull())
00765        {
00766               QString pfad = ScPaths::instance().docDir();
00767               loadHelp(QDir::convertSeparators(pfad + language + "/" + item->text(1)));
00768        }
00769 }
00770 
00771 void HelpBrowser::itemSearchSelected(QListViewItem *item)
00772 {
00773        if (item && !item->text(2).isNull())
00774        {
00775               loadHelp(item->text(2));
00776               findText = searchingEdit->text();
00777               findNext();
00778        }
00779 }
00780 
00781 void HelpBrowser::itemBookmarkSelected(QListViewItem *item)
00782 {
00783        if (item && !item->text(1).isNull())
00784               loadHelp(item->text(1));
00785 }
00786 
00787 
00788 void HelpBrowser::searchingInDirectory(QString aDir)
00789 {
00790        QDir dir(QDir::convertSeparators(aDir + "/"));
00791        QStringList lst = dir.entryList("*.html");
00792        for (QStringList::Iterator it = lst.begin(); it != lst.end(); ++it)
00793        {
00794               QString fname(aDir + (*it));
00795               QFile f(fname);
00796               if (f.open(IO_ReadOnly))
00797               {
00798                      QTextStream stream(&f);
00799                      QString str = stream.read().lower();
00800                      int cnt = str.contains(searchingEdit->text().lower());
00801                      if (cnt > 0)
00802                      {
00803                             // the remove() hack is here for itemSelected() handling
00804                             QString fullname = fname;
00805                             QString title;
00806                             QListViewItem *refItem = listView->findItem(fname.remove(QDir::convertSeparators(ScPaths::instance().docDir()+language + "/")), 1);
00807                             title = refItem ? refItem->text(0) : fname;
00808                             HelpListItem *item = new HelpListItem(searchingView, QString("%2").arg(title), QString("%1").arg(cnt), fullname);
00809                             searchingView->insertItem(item);
00810                      }
00811                      f.close();
00812               }
00813        }
00814        // get dirs - ugly recursion
00815        QStringList dst = dir.entryList("*", QDir::Dirs);
00816        for (QStringList::Iterator it = dst.begin(); it != dst.end(); ++it)
00817               if ((*it)!="." && (*it)!="..")
00818                      searchingInDirectory(QDir::convertSeparators(aDir + QString((*it)) + "/"));
00819 }
00820 
00821 void HelpBrowser::searchingButton_clicked()
00822 {
00823        searchingView->clear();
00824        // root files
00825        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00826        searchingInDirectory(QDir::convertSeparators(ScPaths::instance().docDir() + language + "/"));
00827        QApplication::restoreOverrideCursor();
00828 }
00829 
00830 void HelpBrowser::find()
00831 {
00832        findText = QInputDialog::getText( tr("Find"), tr("Search Term:"), QLineEdit::Normal, findText, 0, this);
00833        if (findText.isNull())
00834               return;
00835        findNext();
00836 }
00837 
00838 void HelpBrowser::findNext()
00839 {
00840        if (findText.isNull())
00841        {
00842               find();
00843               return;
00844        }
00845        // find it. finally
00846        textBrowser->find(findText, false, false, true, 0, 0);
00847 }
00848 
00849 void HelpBrowser::findPrevious()
00850 {
00851        if (findText.isNull())
00852        {
00853               find();
00854               return;
00855        }
00856        // find it. finally
00857        textBrowser->find(findText, false, false, false, 0, 0);
00858 }
00859 
00860 void HelpBrowser::print()
00861 {
00862        QPrinter printer;
00863        printer.setFullPage(true);
00864        if (!printer.setup(this))
00865               return;
00866 
00867        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00868        QPainter p(&printer);
00869        QPaintDeviceMetrics metrics(p.device());
00870        int dpix = metrics.logicalDpiX();
00871        int dpiy = metrics.logicalDpiY();
00872        const int margin = 72; // pt
00873        QRect body(margin*dpix/72, margin*dpiy/72, metrics.width()-margin*dpix/72*2, metrics.height()-margin*dpiy/72*2);
00874        QFont font("Helvetica");
00875        QSimpleRichText richText( textBrowser->text(), font, textBrowser->context(), textBrowser->styleSheet(), textBrowser->mimeSourceFactory(), body.height());
00876        richText.setWidth( &p, body.width());
00877        QRect view(body);
00878        int page = 1;
00879        do {
00880               richText.draw(&p, body.left(), body.top(), view, colorGroup());
00881               view.moveBy(0, body.height());
00882               p.translate(0 , -body.height());
00883               p.setFont(font);
00884               p.drawText(view.right() - p.fontMetrics().width(QString::number(page)), view.bottom() + p.fontMetrics().ascent() + 5, QString::number(page));
00885               if (view.top()  >= body.top() + richText.height())
00886                      break;
00887               printer.newPage();
00888               page++;
00889        } while (true);
00890        QApplication::restoreOverrideCursor();
00891 }
00892 
00893 void HelpBrowser::bookmarkButton_clicked()
00894 {
00895        QString title = textBrowser->documentTitle();
00896        QString fname(QDir::cleanDirPath(textBrowser->source()));
00897        title = QInputDialog::getText( tr("New Bookmark"), tr("New Bookmark's Title:"), QLineEdit::Normal, title, 0, this);
00898        // user cancel
00899        if (title.isNull())
00900               return;
00901        QListViewItem *item = new QListViewItem(bookmarksView, title, fname);
00902        bookmarksView->insertItem(item);
00903 }
00904 
00905 void HelpBrowser::deleteBookmarkButton_clicked()
00906 {
00907        delete(bookmarksView->currentItem());
00908 }
00909 
00910 void HelpBrowser::deleteAllBookmarkButton_clicked()
00911 {
00912        bookmarksView->clear();
00913 }
00914 
00915 void HelpBrowser::readBookmarks()
00916 {
00917        BookmarkParser handler;
00918        handler.view = bookmarksView;
00919        QFile xmlFile(bookmarkFile());
00920        QXmlInputSource source(xmlFile);
00921        QXmlSimpleReader reader;
00922        reader.setContentHandler(&handler);
00923        reader.parse(source);
00924 }
00925 
00926 void HelpBrowser::readHistory()
00927 {
00928        HistoryParser handler;
00929        handler.helpBrowser = this;
00930        QFile xmlFile(historyFile());
00931        QXmlInputSource source(xmlFile);
00932        QXmlSimpleReader reader;
00933        reader.setContentHandler(&handler);
00934        reader.parse(source);
00935 }
00936 
00937 void HelpBrowser::setText(QString str)
00938 {
00939        textBrowser->setText(str);
00940 }