summaryrefslogtreecommitdiffstats
path: root/kviewshell/kviewshell.cpp
blob: b155e73e81a3d184292dd0a22a61c65f5b584794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * Parts of this file are
 * Copyright 2003 Waldo Bastian <bastian@kde.org>
 *
 * These parts are free software; you can redistribute and/or modify
 * them under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.
 */


#include <tqfileinfo.h>
#include <tqtimer.h>
#include <tqregexp.h>

#include <kiconloader.h>
#include <kstandarddirs.h>
#include <kapplication.h>
#include <kaction.h>
#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kmenubar.h>
#include <klibloader.h>
#include <kstdaction.h>
#include <kedittoolbar.h>
#include <kurldrag.h>
#include <tdeparts/partmanager.h>
#include <kmimetype.h>

#include <kprogress.h>
#include <tqlabel.h>

#include <stdlib.h>
#include <unistd.h>

#include "kviewpart_iface.h"
#include <kstatusbar.h>
#include <kkeydialog.h>
#include "kviewshell.moc"


#define StatusBar_ID_PageNr 1
#define StatusBar_ID_PageSize 2
#define StatusBar_ID_Zoom 3


KViewShell::KViewShell(const TQString& defaultMimeType)
  : KParts::MainWindow()
{
  // create the viewer part

  // Try to load
  KLibFactory *factory = KLibLoader::self()->factory("kviewerpart");
  if (factory) {
    if (defaultMimeType == TQString())
    {
      view = (KViewPart_Iface*) factory->create(TQT_TQOBJECT(this), "kviewerpart", "KViewPart");
    }
    else
    {
      TQStringList args;
      args << defaultMimeType;
      view = (KViewPart_Iface*) factory->create(TQT_TQOBJECT(this), "kviewerpart", "KViewPart", args);
    }
    if (!view)
      ::exit(-1);
  } else {
    KMessageBox::error(this, i18n("No viewing component found"));
    ::exit(-1);
  }

  setCentralWidget(view->widget());

  // file menu
  KStdAction::open(TQT_TQOBJECT(view), TQT_SLOT(slotFileOpen()), actionCollection());
  recent = KStdAction::openRecent (TQT_TQOBJECT(this), TQT_SLOT(openURL(const KURL &)), actionCollection());
  reloadAction = new KAction(i18n("Reload"), "reload", CTRL + Key_R, view, TQT_SLOT(reload()), actionCollection(), "reload");
  closeAction = KStdAction::close(TQT_TQOBJECT(this), TQT_SLOT(slotFileClose()), actionCollection());
  KStdAction::quit (TQT_TQOBJECT(this), TQT_SLOT(slotQuit()), actionCollection());

  connect(view, TQT_SIGNAL(fileOpened()), this, TQT_SLOT(addRecentFile()));

  // view menu
  fullScreenAction = KStdAction::fullScreen(TQT_TQOBJECT(this), TQT_SLOT(slotFullScreen()), actionCollection(), this, "fullscreen" );

  // settings menu
  createStandardStatusBarAction();

  setStandardToolBarMenuEnabled(true);

  KStdAction::keyBindings(TQT_TQOBJECT(this), TQT_SLOT(slotConfigureKeys()), actionCollection());
  KStdAction::configureToolbars(TQT_TQOBJECT(this), TQT_SLOT(slotEditToolbar()), actionCollection());

  // statusbar connects
  connect( view, TQT_SIGNAL( zoomChanged(const TQString &) ), this,TQT_SLOT( slotChangeZoomText(const TQString &) ) );
  connect( view, TQT_SIGNAL( pageChanged(const TQString &) ), this,TQT_SLOT( slotChangePageText(const TQString &) ) );
  connect( view, TQT_SIGNAL( sizeChanged(const TQString &) ), this,TQT_SLOT( slotChangeSizeText(const TQString &) ) );

  // Setup session management
  connect( this, TQT_SIGNAL( restoreDocument(const KURL &, int) ), view, TQT_SLOT( restoreDocument(const KURL &, int)));
  connect( this, TQT_SIGNAL( saveDocumentRestoreInfo(TDEConfig*) ), view, TQT_SLOT( saveDocumentRestoreInfo(TDEConfig*)));

  setXMLFile( "kviewshell.rc" );
  createGUI(view);
  readSettings();
  checkActions();
  setAcceptDrops(true);

  // If kviewshell is started when another instance of kviewshell runs
  // in fullscreen mode, the menubar is switched off by default, which
  // is a nasty surprise for the user: there is no way to access the
  // menus. To avoid such complications, we switch the menubar on
  // explicitly.
  menuBar()->show();

  // Add statusbar-widgets for zoom, pagenr and format
  statusBar()->insertItem("", StatusBar_ID_PageNr, 0, true);
  statusBar()->insertFixedItem("XXXX%", StatusBar_ID_Zoom, true);
  statusBar()->changeItem("", StatusBar_ID_Zoom);
  statusBar()->insertItem(view->pageSizeDescription(), StatusBar_ID_PageSize, 0, true);

  connect( view, TQT_SIGNAL(pluginChanged(KParts::Part*)), this, TQT_SLOT(createGUI(KParts::Part*)));
}


void KViewShell::checkActions()
{
  bool doc = !view->url().isEmpty();

  closeAction->setEnabled(doc);
  reloadAction->setEnabled(doc);
  fullScreenAction->setEnabled(doc);
}


KViewShell::~KViewShell()
{
  writeSettings();
  delete view;
}


void KViewShell::slotQuit()
{
  // If we are to quit the application while we operate in fullscreen
  // mode, we need to restore the visibility properties of the
  // statusbar, toolbar and the menus because these properties are
  // saved automatically ... and we don't want to start next time
  // without having menus.
  if (fullScreenAction->isChecked()) {
    kdDebug() << "Switching off fullscreen mode before quitting the application" << endl;
    showNormal();
    if (isStatusBarShownInNormalMode)
      statusBar()->show();
    if (isToolBarShownInNormalMode)
      toolBar()->show();
    menuBar()->show();
    view->slotSetFullPage(false); 
  }
  kapp->closeAllWindows();
  kapp->quit();
}


void KViewShell::readSettings()
{
  resize(600, 300); // default size if the config file specifies no size
  setAutoSaveSettings( "General" ); // apply mainwindow settings (size, toolbars, etc.)

  TDEConfig *config = kapp->config();
  config->setGroup("General");

  recent->loadEntries(config, "Recent Files");

  // Constant source of annoyance in KDVI < 1.0: the 'recent-files'
  // menu contains lots of files which don't exist (any longer). Thus,
  // we'll sort out the non-existent files here.
  TQStringList items = recent->items();
  for ( TQStringList::Iterator it = items.begin(); it != items.end(); ++it ) {
    KURL url(*it);
    if (url.isLocalFile()) {
      TQFileInfo info(url.path());
      if (!info.exists())
        recent->removeURL(url);
    }
  }

}


void KViewShell::writeSettings()
{
  TDEConfig *config = kapp->config();
  config->setGroup( "General" );
  recent->saveEntries(config, "Recent Files");

  config->sync();
}


void KViewShell::saveProperties(TDEConfig* config)
{
  // the 'config' object points to the session managed
  // config file.  anything you write here will be available
  // later when this app is restored
  emit saveDocumentRestoreInfo(config);
}


void KViewShell::readProperties(TDEConfig* config)
{
  // the 'config' object points to the session managed
  // config file.  this function is automatically called whenever
  // the app is being restored.  read in here whatever you wrote
  // in 'saveProperties'
  if (view)
  {
    KURL url (config->readPathEntry("URL"));
    if (url.isValid())
      emit restoreDocument(url, config->readNumEntry("Page", 1));
  }
}


void KViewShell::addRecentFile()
{
  // Get the URL of the opened file from the kviewpart.
  KURL actualURL = view->url();
  // To store the URL in the list of recent files, we remove the
  // reference part.
  actualURL.setRef(TQString());
  recent->addURL(actualURL);
  checkActions();
}

void KViewShell::openURL(const KURL& url)
{
  view->openURL(url);
}

void KViewShell::slotFullScreen()
{
  if (fullScreenAction->isChecked()) {
    // In fullscreen mode, menu- tool- and statusbar are hidden. Save
    // the visibility flags of these objects here, so that they can
    // later be properly restored when we switch back to normal mode,
    // or before we leave the application in slotQuit()
    isStatusBarShownInNormalMode = statusBar()->isShown();
    statusBar()->hide();
    isToolBarShownInNormalMode = toolBar()->isShown();
    toolBar()->hide();
    menuBar()->hide();
    view->slotSetFullPage(true); 

    // Go to fullscreen mode
    showFullScreen();

    KMessageBox::information(this, i18n("Use the Escape key to leave the fullscreen mode."), i18n("Entering Fullscreen Mode"), "leavingFullScreen");
  } else {
    showNormal();
    if (isStatusBarShownInNormalMode)
      statusBar()->show();
    if (isToolBarShownInNormalMode)
      toolBar()->show();
    menuBar()->show();
    view->slotSetFullPage(false); 
  }
}


void KViewShell::slotFileClose()
{
  view->closeURL_ask();

  checkActions();
}

void KViewShell::slotConfigureKeys()
{
  KKeyDialog dlg( true, this );

  dlg.insert( actionCollection() );
  dlg.insert( view->actionCollection() );

  dlg.configure();
}

void KViewShell::slotEditToolbar()
{
  saveMainWindowSettings( TDEGlobal::config(), autoSaveGroup() );
  KEditToolbar dlg(factory());
  connect( &dlg, TQT_SIGNAL( newToolbarConfig() ), TQT_SLOT( slotNewToolbarConfig() ) );
  dlg.exec();
}


void KViewShell::slotNewToolbarConfig()
{
  applyMainWindowSettings( TDEGlobal::config(), autoSaveGroup() );
}

void KViewShell::dragEnterEvent(TQDragEnterEvent *event)
{
  if (KURLDrag::canDecode(event))
  {
    KURL::List urls;
    KURLDrag::decode(event, urls);
    if (!urls.isEmpty())
    {
      KURL url = urls.first();

      // Always try to open remote files
      if (!url.isLocalFile())
      {
        event->accept();
        return;
      }

      // For local files we only accept a drop, if we have a plugin for its
      // particular mimetype
      KMimeType::Ptr mimetype = KMimeType::findByURL(url);
      kdDebug() << "[dragEnterEvent] Dragged URL is of type " << mimetype->comment() << endl;

      // Safety check
      if (view)
      {
        TQStringList mimetypeList = view->supportedMimeTypes();
        kdDebug() << "[dragEnterEvent] Supported mime types: " << mimetypeList << endl;

        for (TQStringList::Iterator it = mimetypeList.begin(); it != mimetypeList.end(); ++it)
        {
          if (mimetype->is(*it))
          {
            kdDebug() << "[dragEnterEvent] Found matching mimetype: " << *it << endl;
            event->accept();
            return;
          }
        }
        kdDebug() << "[dragEnterEvent] no matching mimetype found" << endl;
      }
    }
    event->ignore();
  }
}


void KViewShell::dropEvent(TQDropEvent *event)
{
  KURL::List urls;
  if (KURLDrag::decode(event, urls) && !urls.isEmpty())
    view->openURL(urls.first());
}


void KViewShell::keyPressEvent(TQKeyEvent *event)
{
  // The Escape Key is used to return to normal mode from fullscreen
  // mode
  if ((event->key() == TQt::Key_Escape) && (fullScreenAction->isChecked())) {
    showNormal();
    return;
  }
  // If we can't use the key event, pass it on
  event->ignore();
}


void KViewShell::slotChangePageText(const TQString &message)
{
  statusBar()->changeItem(" "+message+" ",StatusBar_ID_PageNr);
}


void KViewShell::slotChangeSizeText(const TQString &message)
{
  statusBar()->changeItem(" "+message+" ",StatusBar_ID_PageSize);
}


void KViewShell::slotChangeZoomText(const TQString &message)
{
  statusBar()->changeItem(" "+message+" ",StatusBar_ID_Zoom);
}