~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to koshell/koshell_shell.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "koshell_shell.h"
26
26
 
 
27
#include <ktempfile.h>
27
28
#include <kfiledialog.h>
28
29
#include <klocale.h>
29
30
#include <kdebug.h>
30
 
#include <kstddirs.h>
 
31
#include <kstandarddirs.h>
31
32
#include <klibloader.h>
32
33
#include <kservice.h>
33
34
#include <kmessagebox.h>
34
35
#include <krecentdocument.h>
 
36
#include <kparts/partmanager.h>
 
37
#include <kaction.h>
35
38
 
36
39
#include <koQueryTrader.h>
37
40
#include <koKoolBar.h>
40
43
#include <koView.h>
41
44
#include <koPartSelectDia.h>
42
45
#include <koFilterManager.h>
 
46
#include <kiconloader.h>
43
47
 
44
48
KoShellWindow::KoShellWindow()
45
49
 : KoMainWindow( KGlobal::instance() )
46
50
{
47
51
  m_activePage = m_lstPages.end();
48
52
 
49
 
  m_pLayout = new QHBox( this );
 
53
  m_pLayout = new QHBox( centralWidget() );
50
54
 
51
55
  m_pKoolBar = new KoKoolBar( m_pLayout );
52
56
 
53
 
  setCentralWidget( m_pLayout );
54
 
 
55
57
  m_pFrame = new KoShellFrame( m_pLayout );
56
58
 
57
59
  m_grpFile = m_pKoolBar->insertGroup(i18n("Parts"));
58
 
  QValueList<KoDocumentEntry> lstComponents = KoDocumentEntry::query();
 
60
  QValueList<KoDocumentEntry> lstComponents = KoDocumentEntry::query(false,QString());
59
61
  QValueList<KoDocumentEntry>::Iterator it = lstComponents.begin();
60
62
  for( ; it != lstComponents.end(); ++it )
61
63
  {
62
 
      //kdDebug() << "Inserting into koolbar : " << (*it).name << endl;
63
 
      int id = m_pKoolBar->insertItem( m_grpFile,
 
64
      //kdDebug() << "Inserting into koolbar : " << (*it).name() << endl;
 
65
      int id;
 
66
      if (!(*it).service()->genericName().isEmpty()) //skip the unavailable part
 
67
          id = m_pKoolBar->insertItem( m_grpFile,
64
68
                                       DesktopIcon((*it).service()->icon()),
65
69
                                       (*it).name(),
66
70
                                       this, SLOT( slotKoolBar( int, int ) ) );
 
71
      else
 
72
          continue;
 
73
 
67
74
      m_mapComponents[ id ] = *it;
68
75
 
69
76
      // Build list of patterns for all supported KOffice documents...
87
94
  m_pKoolBar->setFixedWidth( 80 );
88
95
  m_pKoolBar->setMinimumHeight( 300 );
89
96
 
 
97
  connect( this, SIGNAL( documentSaved() ),
 
98
           this, SLOT( slotNewDocumentName() ) );
 
99
 
90
100
  // Not implemented yet
91
101
  actionCollection()->action("view_split")->setEnabled(false);
92
102
  actionCollection()->action("view_splitter_orientation")->setEnabled(false);
 
103
 
 
104
  m_client = new KoShellGUIClient( this );
 
105
  createShellGUI();
93
106
}
94
107
 
95
108
KoShellWindow::~KoShellWindow()
96
109
{
97
110
  //kdDebug() << "KoShellWindow::~KoShellWindow()" << endl;
98
111
 
 
112
  // Set the active part to 0 (as we do in ~KoMainWindow, but this is too
 
113
  // late for KoShell, it gets activePartChanged signals delivered to a dead
 
114
  // KoShellWindow object).
 
115
  partManager()->setActivePart(0);
 
116
 
99
117
  // Destroy all documents - queryClose has made sure we saved them first
100
118
  QValueList<Page>::ConstIterator it = m_lstPages.begin();
101
119
  for (; it != m_lstPages.end(); ++it )
107
125
  }
108
126
  m_lstPages.clear();
109
127
 
110
 
  setRootDocumentDirect( 0L, QList<KoView>() ); // prevent our parent destructor from doing stupid things
111
 
}
112
 
 
113
 
QString KoShellWindow::nativeFormatName() const
114
 
{
115
 
  return i18n("KOffice Documents");
116
 
}
117
 
 
118
 
QString KoShellWindow::nativeFormatPattern() const
119
 
{
120
 
  return m_filter; // "*.kwd *.ksp *.kpr";
121
 
}
122
 
 
123
 
bool KoShellWindow::openDocument( const KURL & url )
124
 
{
125
 
    return openDocumentInternalKoShell( 0L, url );
126
 
}
127
 
 
128
 
// Should be openDocumentInternal if it was virtual in KoMainWindow
129
 
bool KoShellWindow::openDocumentInternalKoShell( KoFilterManager * filterManager, const KURL &url )
130
 
{
 
128
  setRootDocumentDirect( 0L, QPtrList<KoView>() ); // prevent our parent destructor from doing stupid things
 
129
}
 
130
 
 
131
bool KoShellWindow::openDocumentInternal( const KURL &url, KoDocument* )
 
132
{
 
133
  // Here we have to distinguish two cases: The passed URL has a native
 
134
  // KOffice mimetype. Then we query the trader and create the document.
 
135
  // The file is loaded and everyone's happy.
 
136
  // The second case is a non-native file. Here we have to create a
 
137
  // filter manager, ask it to convert the file to the "closest" available
 
138
  // KOffice part and open the temporary file.
131
139
  KMimeType::Ptr mimeType = KMimeType::findByURL( url );
132
140
  m_documentEntry = KoDocumentEntry::queryByMimeType( mimeType->name().latin1() );
133
 
  if ( m_documentEntry.isEmpty() )
134
 
    return false;
 
141
 
 
142
  KTempFile* tmpFile = 0;
 
143
  KURL tmpUrl( url );  // we might have to load a converted temp. file
 
144
 
 
145
  if ( m_documentEntry.isEmpty() ) { // non-native
 
146
    tmpFile = new KTempFile;
 
147
 
 
148
    KoFilterManager *manager = new KoFilterManager( url.path() );
 
149
    QCString mimetype;                                               // an empty mimetype means, that the "nearest"
 
150
    KoFilter::ConversionStatus status = manager->exp0rt( tmpFile->name(), mimetype ); // KOffice part will be chosen
 
151
    delete manager;
 
152
 
 
153
    if ( status != KoFilter::OK || mimetype.isEmpty() ) {
 
154
      tmpFile->unlink();
 
155
      delete tmpFile;
 
156
      return false;
 
157
    }
 
158
 
 
159
    // If the conversion was successful we get the mimetype of the
 
160
    // chosen KOffice part back.
 
161
    m_documentEntry = KoDocumentEntry::queryByMimeType( mimetype );
 
162
    if ( m_documentEntry.isEmpty() ) {
 
163
      tmpFile->unlink();
 
164
      delete tmpFile;
 
165
      return false;
 
166
    }
 
167
 
 
168
    // Open the temporary file
 
169
    tmpUrl.setPath( tmpFile->name() );
 
170
  }
135
171
 
136
172
  m_recent->addURL( url );
137
173
 
138
174
  KoDocument* newdoc = m_documentEntry.createDoc();
139
175
 
140
 
  // Pass the filterManager to the document (who will own it from now on)
141
 
  if ( filterManager )
142
 
      newdoc->setFilterManager( filterManager );
143
 
 
144
176
  connect(newdoc, SIGNAL(sigProgress(int)), this, SLOT(slotProgress(int)));
145
177
  connect(newdoc, SIGNAL(completed()), this, SLOT(slotKSLoadCompleted()));
146
178
  connect(newdoc, SIGNAL(canceled( const QString & )), this, SLOT(slotKSLoadCanceled( const QString & )));
147
 
  if ( !newdoc || !newdoc->openURL( url ) )
 
179
  newdoc->addShell( this ); // used by openURL
 
180
  bool openRet = (!isImporting ()) ? newdoc->openURL(tmpUrl) : newdoc->import(tmpUrl);
 
181
  if ( !newdoc || !openRet )
148
182
  {
149
183
      delete newdoc;
 
184
      if ( tmpFile ) {
 
185
        tmpFile->unlink();
 
186
        delete tmpFile;
 
187
      }
150
188
      return false;
151
189
  }
 
190
 
 
191
  if ( tmpFile ) {
 
192
    //if the laoded file has been a temporary file
 
193
    //we need to correct a few document settings
 
194
    //see description of bug #77574 for additional information
 
195
  
 
196
    //correct (output) mime type: we need to set it to the non-native format
 
197
    //to make sure the user knows about saving to a non-native mime type
 
198
    //setConfirmNonNativeSave is set to true below
 
199
    newdoc->setMimeType( mimeType->name().latin1() );
 
200
    newdoc->setOutputMimeType( mimeType->name().latin1() );
 
201
    
 
202
    //the next time the user saves the document he should be warned
 
203
    //because of mime type settings done above;
 
204
    newdoc->setConfirmNonNativeSave(true,true); //exporting,warn_on
 
205
    newdoc->setConfirmNonNativeSave(false,true); //save/save as,warn_on
 
206
    
 
207
    //correct document file (should point to URL)
 
208
    newdoc->setFile( url.path() );
 
209
    
 
210
    //correct document URL
 
211
    newdoc->setURL( url );
 
212
    
 
213
    //update caption to represent the correct URL in the window titlebar
 
214
    updateCaption();
 
215
  
 
216
    tmpFile->unlink();
 
217
    delete tmpFile;
 
218
  }
152
219
  return true;
153
220
}
154
221
 
156
223
void KoShellWindow::slotKSLoadCompleted()
157
224
{
158
225
    KoDocument* newdoc = (KoDocument *)(sender());
 
226
 
 
227
    // KoDocument::import() calls resetURL() too late...
 
228
    // ...setRootDocument will show the URL...
 
229
    // So let's stop this from happening and the user will never know :)
 
230
    if (isImporting()) newdoc->resetURL ();
 
231
 
159
232
    partManager()->addPart( newdoc, false );
160
233
    setRootDocument( newdoc );
161
234
    disconnect(newdoc, SIGNAL(sigProgress(int)), this, SLOT(slotProgress(int)));
167
240
{
168
241
    KMessageBox::error( this, errMsg );
169
242
    // ... can't delete the document, it's the one who emitted the signal...
 
243
    // ###### FIXME: This can be done in 3.0 with deleteLater, I assume (Werner)
170
244
 
171
245
    KoDocument* newdoc = (KoDocument *)(sender());
172
246
    disconnect(newdoc, SIGNAL(sigProgress(int)), this, SLOT(slotProgress(int)));
184
258
 
185
259
  if ( doc )
186
260
  {
187
 
    doc->addShell( this );
 
261
    if ( !doc->shells().contains( this ) )
 
262
        doc->addShell( this );
188
263
    KoView *v = doc->createView( m_pFrame );
189
 
    QList<KoView> views;
 
264
    QPtrList<KoView> views;
190
265
    views.append(v);
191
266
    setRootDocumentDirect( doc, views );
192
267
 
200
275
    page.m_pView = v;
201
276
    page.m_id = m_pKoolBar->insertItem( m_grpDocuments,
202
277
                                        DesktopIcon( m_documentEntry.service()->icon() ),
203
 
                                        i18n("No name"),
 
278
                                        i18n("Untitled"),
204
279
                                        this, SLOT( slotKoolBar( int, int ) ) );
205
280
    kdDebug() << " New page has id " << page.m_id << " doc is " << doc << endl;
206
281
 
209
284
    switchToPage( m_lstPages.fromLast() );
210
285
  } else
211
286
  {
212
 
    setRootDocumentDirect( 0L, QList<KoView>() );
 
287
    setRootDocumentDirect( 0L, QPtrList<KoView>() );
213
288
    m_activePage = m_lstPages.end();
214
289
    KoMainWindow::updateCaption();
215
290
  }
216
291
}
217
292
 
 
293
void KoShellWindow::slotNewDocumentName()
 
294
{
 
295
        updateCaption();
 
296
}
 
297
 
218
298
void KoShellWindow::updateCaption()
219
299
{
220
300
    kdDebug() << "KoShellWindow::updateCaption() rootDoc=" << rootDocument() << endl;
231
311
        QString name;
232
312
        if ( rootDocument()->documentInfo() )
233
313
        {
234
 
            KoDocumentInfoPage * page = rootDocument()->documentInfo()->page( QString::fromLatin1("about"));
235
 
            if (page)
236
 
                name = static_cast<KoDocumentInfoAbout *>(page)->title();
 
314
            name = rootDocument()->documentInfo()->title();
237
315
        }
238
316
        if ( name.isEmpty() )
239
 
          // Fall back to document URL
240
 
          name = rootDocument()->url().fileName();
 
317
            // Fall back to document URL
 
318
            name = rootDocument()->url().fileName();
241
319
 
242
 
        if ( !name.isEmpty() ) // else keep No name
 
320
        if ( !name.isEmpty() ) // else keep Untitled
243
321
        {
244
 
          if ( name.length() > 20 )
245
 
          {
246
 
            name.truncate( 17 );
247
 
            name += "...";
248
 
          }
249
 
 
250
 
          m_pKoolBar->renameItem( m_grpDocuments, (*it).m_id, name );
 
322
            if ( name.length() > 20 )
 
323
            {
 
324
                name.truncate( 17 );
 
325
                name += "...";
 
326
            }
 
327
            m_pKoolBar->renameItem( m_grpDocuments, (*it).m_id, name );
251
328
        }
252
329
 
253
330
        return;
267
344
    KoDocument *doc = m_documentEntry.createDoc();
268
345
    if (doc)
269
346
    {
 
347
        // koshell isn't starting, but this is like starting a new app:
 
348
        // offer both "open existing file" and "open new file".
 
349
        doc->setInitDocFlags( KoDocument::InitDocAppStarting );
270
350
        if ( doc->initDoc() )
271
351
        {
272
352
            partManager()->addPart( doc, false );
296
376
  }
297
377
}
298
378
 
 
379
void KoShellWindow::slotShowSidebar()
 
380
{
 
381
        if( m_pKoolBar->isShown() )
 
382
                m_pKoolBar->hide();
 
383
        else
 
384
                m_pKoolBar->show();
 
385
}
 
386
 
299
387
void KoShellWindow::switchToPage( QValueList<Page>::Iterator it )
300
388
{
301
389
  // Move away current page (view)
311
399
  // Make it active (GUI etc.)
312
400
  partManager()->setActivePart( (*m_activePage).m_pDoc, v );
313
401
  // Change current document
314
 
  QList<KoView> views;
 
402
  QPtrList<KoView> views;
315
403
  views.append(v);
316
404
  setRootDocumentDirect( (*m_activePage).m_pDoc, views );
317
 
  // Fix caption
 
405
  // Fix caption and set focus to the new view
318
406
  updateCaption();
 
407
  v->setFocus();
319
408
}
320
409
 
321
410
void KoShellWindow::slotFileNew()
324
413
    if ( m_documentEntry.isEmpty() )
325
414
      return;
326
415
    KoDocument* newdoc = m_documentEntry.createDoc();
327
 
    if ( !newdoc || !newdoc->initDoc() )
 
416
    if ( !newdoc )
 
417
        return;
 
418
    newdoc->setInitDocFlags( KoDocument::InitDocFileNew );
 
419
    if ( !newdoc->initDoc() )
328
420
    {
329
421
      delete newdoc;
330
422
      return;
334
426
    setRootDocument( newdoc );
335
427
}
336
428
 
337
 
// ### This is an ugly copy of KoMainWindow::slotFileOpen, due to the fact
338
 
// that openDocumentInternal isn't virtual
339
429
void KoShellWindow::slotFileOpen()
340
430
{
341
431
    KFileDialog *dialog=new KFileDialog(QString::null, QString::null, 0L, "file dialog", true);
342
 
    dialog->setCaption( i18n("Open document") );
343
 
    KoFilterManager * filterManager = new KoFilterManager;
344
 
    filterManager->prepareDialog(dialog, KoFilterManager::Import,
345
 
                                 KoDocument::readNativeFormatMimeType(),
346
 
                                 nativeFormatPattern(), nativeFormatName(), true);
 
432
    if (!isImporting())
 
433
        dialog->setCaption( i18n("Open Document") );
 
434
    else
 
435
        dialog->setCaption( i18n("Import Document") );
 
436
    dialog->setMimeFilter( KoFilterManager::mimeFilter() );
 
437
 
347
438
    KURL url;
348
439
    if(dialog->exec()==QDialog::Accepted) {
349
440
        url=dialog->selectedURL();
354
445
            KRecentDocument::add(url.url(-1), true);
355
446
    }
356
447
    else
357
 
    {
358
 
        delete filterManager;
359
448
        return;
360
 
    }
361
449
 
362
 
    filterManager->cleanUp();
363
450
    delete dialog;
364
451
    if ( url.isEmpty() )
365
 
    {
366
 
        delete filterManager;
367
452
        return;
368
 
    }
369
453
 
370
 
    (void) openDocumentInternalKoShell( filterManager, url );
 
454
    (void) openDocumentInternal( url );
371
455
}
372
456
 
373
457
void KoShellWindow::slotFileClose()
426
510
bool KoShellWindow::queryClose()
427
511
{
428
512
  // Save current doc and views
429
 
  QList<KoView> currentViews;
 
513
  QPtrList<KoView> currentViews;
430
514
  KoDocument * currentDoc = 0L;
431
515
  bool ok = true;
432
516
  if (m_activePage != m_lstPages.end())
433
517
  {
434
518
      currentDoc = (*m_activePage).m_pDoc;
435
519
      currentViews.append((*m_activePage).m_pView);
436
 
  
 
520
 
437
521
      // This one is called by slotFileQuit and by the X button.
438
522
      // We have to check for unsaved docs...
439
 
      
 
523
 
440
524
      QValueList<Page>::Iterator it = m_lstPages.begin();
441
525
      for( ; it != m_lstPages.end(); ++it )
442
526
      {
443
527
          // This is quite a HACK
444
528
          // We should ask ourselves, to get a better dialog box
445
 
          setRootDocumentDirect( (*it).m_pDoc, QList<KoView>() );
 
529
          setRootDocumentDirect( (*it).m_pDoc, QPtrList<KoView>() );
446
530
          // Test if we can close this doc
447
531
          if ( !KoMainWindow::queryClose() )
448
532
          {
451
535
          }
452
536
      }
453
537
 
454
 
      // Restore current doc and views
455
 
      setRootDocumentDirect( currentDoc, currentViews );
 
538
  // Restore current doc and views
 
539
  setRootDocumentDirect( currentDoc, currentViews );
456
540
  }
457
541
  return ok;
458
542
}
459
543
 
460
544
/*
461
 
// Should this be an additionnal action in the File menu ?
 
545
// Should this be an additional action in the File menu ?
462
546
bool KoShellWindow::saveAllPages()
463
547
{
464
548
  // TODO
481
565
}
482
566
*/
483
567
 
 
568
void KoShellWindow::createShellGUI()
 
569
{
 
570
        guiFactory()->addClient( m_client );
 
571
}
 
572
 
484
573
///////
485
574
 
486
575
KoShellFrame::KoShellFrame( QWidget *parent )
500
589
    m_pView->setGeometry( 0, 0, width(), height() );
501
590
}
502
591
 
 
592
KoShellGUIClient::KoShellGUIClient( KoShellWindow *window ) : KXMLGUIClient()
 
593
{
 
594
        setXMLFile( "koshellui.rc", true, false );
 
595
 
 
596
        sidebar = new KToggleAction(i18n("Show Sidebar"), "view_choose", 0, window,
 
597
                        SLOT( slotShowSidebar() ), actionCollection(), "show_sidebar");
 
598
        sidebar->setChecked( true );
 
599
}
 
600
 
503
601
#include "koshell_shell.moc"