summaryrefslogtreecommitdiffstats
path: root/quanta/parts/preview/whtmlpart.cpp
blob: 6560191bbafc0b0ad94e1d3f70afe1810aaf5c47 (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
/***************************************************************************
                          whtmlpart.cpp  -  description
                             -------------------
    begin                : Fri Aug 18 2000
    copyright            : (C) 2000 by Dmitry Poplavsky & Alexander Yakovlev & Eric Laffoon <[email protected],[email protected],[email protected]>
                           (C) 2002, 2004, 2005 Andras Mantia <[email protected]>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
//qt includes
#include <tqfileinfo.h>
#include <tqtextcodec.h>

//kde includes
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeversion.h>
#include <tdehtml_settings.h>
#include <tdehtmlview.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <ktempfile.h>

//app includes
#include "whtmlpart.h"
#include "resource.h"

WHTMLPart::WHTMLPart(TQWidget *parentWidget, const char *widgetName, bool enableViewSource, 
            TQObject *parent, const char *name, GUIProfile prof)
  : TDEHTMLPart(parentWidget, widgetName, parent, name, prof), m_contextMenu(0)
{
  //kdDebug(24000) << "WHTMLPart: " << parentWidget << " " << widgetName << " " << parent << " " << name << this << endl;
   hpos = 0;
   // get settings from konq.
   TDEConfig konqConfig("konquerorrc");

   konqConfig.setGroup("HTML Settings");

   const TDEHTMLSettings * set = settings();

   const_cast<TDEHTMLSettings*>(set)->init( &konqConfig, false );
   view()->installEventFilter(this);
   
   m_enableViewSource = enableViewSource;
   if (m_enableViewSource)
   {
    m_contextMenu = new TDEPopupMenu(parentWidget);
    m_contextMenu->insertItem(i18n("View &Document Source"), this, TQT_SLOT(slotViewSource()));
    
    connect(this, TQT_SIGNAL(popupMenu(const TQString&, const TQPoint&)), TQT_SLOT(popupMenu(const TQString&, const TQPoint&)));
   }
   connect(browserExtension(), TQT_SIGNAL(openURLRequest (const KURL &, const KParts::URLArgs &)), this, TQT_SLOT(openURL(const KURL&)));

//   setCharset( konqConfig.readEntry("DefaultEncoding") );
//   setEncoding( konqConfig.readEntry("DefaultEncoding") );
//   setStandardFont( konqConfig.readEntry("StandardFont") );
//   setFixedFont( konqConfig.readEntry("FixedFont") );
//   updateFontSize( konqConfig.readNumEntry("FontSize"));
}

WHTMLPart::~WHTMLPart()
{
}

void WHTMLPart::setPreviewedURL(const KURL &url)
{
  m_previewedURL = url;
}

bool WHTMLPart::openURL(const KURL& url)
{
  if (url == m_previewedURL)
  {
    KURL previewURL = url;
    previewURL.setFileName("preview-" + url.fileName());
    return TDEHTMLPart::openURL(previewURL);
  } else
    return TDEHTMLPart::openURL(url);
}

void  WHTMLPart::urlSelected ( const TQString &url, int button, int state, const TQString &target, KParts::URLArgs args)
{
  TDEHTMLPart::urlSelected (url, button, state, target, args);
  KURL cURL = completeURL( url );
//  alternative not tested but used in tdevelop !
//  KURL cURL=KURL(baseURL(),url);
  if (target.isEmpty() || (target == "_self") || (target == "_top") || (target == "_blank") || (target == "_parent") )
    openURL( cURL ) ;
  addToHistory( cURL.url() );
}

void WHTMLPart::forward()
{
  if ( forwardEnable() ) {
    hpos++;
    openURL( KURL( history.at(hpos) ) );

    emit updateStatus( backEnable() , forwardEnable() );
  }
}


void WHTMLPart::back()
{
  if (backEnable())
  {
    hpos--;
    openURL(KURL(history.at(hpos)));

    emit updateStatus(backEnable(), forwardEnable());
  }
}


void WHTMLPart::addToHistory(const TQString &url)
{

   if ( history.count() > 0 )
     while ( hpos < history.count()-1  )
         history.removeLast();

   if ( !history.isEmpty() ) hpos++;

   history.append(url.ascii());

   hpos = history.count()-1;

   emit updateStatus( backEnable() , forwardEnable() );

}


bool WHTMLPart::backEnable()
{
   return hpos > 0;
}


bool WHTMLPart::forwardEnable()
{
   return hpos < history.count()-1;
}

KParts::ReadOnlyPart *WHTMLPart::createPart( TQWidget * parentWidget, const char *widgetName, 
                                            TQObject *parent, const char *name,
                                            const TQString &, TQString &,
                                            TQStringList &, const TQStringList &)
{
  //kdDebug(24000) << "Create WHTMLPart: " << parentWidget << " " << widgetName << " " << parent << " " << name << endl;
  return new WHTMLPart(parentWidget, widgetName, m_enableViewSource, parent, name);
}

bool WHTMLPart::eventFilter(TQObject *watched, TQEvent *e)
{
  if (TQT_BASE_OBJECT(watched) == TQT_BASE_OBJECT(view()) && e->type() == TQEvent::FocusOut && (!m_contextMenu || !m_contextMenu->hasFocus()))
    emit previewHasFocus(false);
  else
  if (TQT_BASE_OBJECT(watched) == TQT_BASE_OBJECT(view()) && e->type() == TQEvent::FocusIn)
    emit previewHasFocus(true);
  return false;
}

void WHTMLPart::popupMenu(const TQString &/*url*/, const TQPoint &point)
{
  m_contextMenu->popup(point);  
}

void WHTMLPart::slotViewSource()
{  
  KTempFile *tmpFile = new KTempFile(tmpDir + "-preview-", ".html");
  TQString tempFileName = TQFileInfo(*(tmpFile->file())).filePath();
  tmpFile->setAutoDelete(true);
  tmpFile->textStream()->setCodec(TQTextCodec::codecForName("utf8"));
  *(tmpFile->textStream()) << documentSource();
  tmpFile->close();
  tempFileList.append(tmpFile);
  emit showPreview(false);
  emit openFile(KURL::fromPathOrURL(tmpFile->name()), "utf8", true);
}

#include "whtmlpart.moc"