1 /***************************************************************************
2 * Copyright 2006 Adam Treat <treat@kde.org> *
3 * Copyright 2007 Alexander Dymo <adymo@kdevelop.org> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "partcontroller.h"
24 #include <sys/types.h>
28 #include <QMutexLocker>
32 #include <kmimetype.h>
33 #include <kmimetypetrader.h>
35 #include <kparts/part.h>
36 #include <kparts/factory.h>
37 #include <kparts/partmanager.h>
38 #include <kparts/browserextension.h>
40 #include <ktexteditor/view.h>
41 #include <ktexteditor/editor.h>
42 #include <ktexteditor/document.h>
43 #include <ktexteditor/factory.h>
44 #include <ktexteditor/smartinterface.h>
47 // #include "kdevmainwindow.h"
48 // #include "kdevconfig.h"
53 class PartControllerPrivate
56 PartControllerPrivate(): m_textEditor(0) {}
59 QStringList m_textTypes
;
63 KTextEditor::Editor
*m_textEditor
;
65 KParts::Factory
*findPartFactory( const QString
&mimeType
,
66 const QString
&partType
,
67 const QString
&preferredName
= QString() )
70 KService::List offers
= KMimeTypeTrader::self() ->query(
72 "KParts/ReadOnlyPart",
73 QString( "'%1' in ServiceTypes" ).arg( partType
) );
75 if ( offers
.count() > 0 )
78 // if there is a preferred plugin we'll take it
79 if ( !preferredName
.isEmpty() )
81 KService::List::ConstIterator it
;
82 for ( it
= offers
.begin(); it
!= offers
.end(); ++it
)
84 if ( ( *it
) ->desktopEntryName() == preferredName
)
90 // else we just take the first in the list
95 KParts::Factory
*factory
= static_cast<KParts::Factory
*>(
96 KLibLoader::self() ->factory(
97 QFile::encodeName( ptr
->library() ) ) );
106 PartController::PartController(Core
*core
, QWidget
*toplevel
)
107 : KParts::PartManager( toplevel
, 0 ), d(new PartControllerPrivate
)
111 //Cache this as it is too expensive when creating parts
112 // KConfig * config = Config::standard();
113 // config->setGroup( "General" );
115 // d->m_textTypes = config->readEntry( "TextTypes", QStringList() );
117 // config ->setGroup( "Editor" );
118 // d->m_editor = config->readPathEntry( "EmbeddedKTextEditor", QString() );
121 PartController::~PartController()
126 //MOVE BACK TO DOCUMENTCONTROLLER OR MULTIBUFFER EVENTUALLY
127 bool PartController::isTextType( KMimeType::Ptr mimeType
)
129 bool isTextType
= false;
130 if ( d
->m_textTypes
.contains( mimeType
->name() ) )
135 bool isKDEText
= false;
136 QVariant v
= mimeType
->property( "X-KDE-text" );
138 isKDEText
= v
.toBool();
140 // is this regular text - open in editor
141 return ( isTextType
|| isKDEText
142 || mimeType
->is( "text/plain" )
143 || mimeType
->is( "text/html" )
144 || mimeType
->is( "application/x-zerosize" ) );
147 KTextEditor::Document
* PartController::createTextPart(const QString
&encoding
)
149 if (!d
->m_textEditor
)
151 KTextEditor::Factory
* editorFactory
= qobject_cast
<KTextEditor::Factory
*>(d
->findPartFactory(
153 "KTextEditor/Document",
154 "KTextEditor::Editor" ));
156 d
->m_textEditor
= editorFactory
->editor();
158 KTextEditor::Document
* doc
= d
->m_textEditor
->createDocument(this);
160 if ( !encoding
.isNull() )
162 KParts::OpenUrlArguments args
= doc
->arguments();
163 args
.setMimeType( QString( "text/plain;" ) + encoding
);
164 doc
->setArguments( args
);
170 void PartController::removePart( KParts::Part
*part
)
173 if ( KTextEditor::Document
* doc
= qobject_cast
<KTextEditor::Document
*>( part
) )
175 if ( KTextEditor::SmartInterface
* smart
= dynamic_cast<KTextEditor::SmartInterface
*>( doc
) )
177 // FIXME not supposed to delete locked mutexes...
178 QMutexLocker
lock ( smart
->smartMutex() );
179 KParts::PartManager::removePart( part
);
183 kWarning() << "Deleting text editor" << doc
<< "which does not have a smart interface." ;
187 KParts::PartManager::removePart( part
);
190 KParts::Part
* PartController::createPart( const QString
& mimeType
,
191 const QString
& partType
,
192 const QString
& className
,
193 const QString
& preferredName
)
195 KParts::Factory
* editorFactory
= d
->findPartFactory(
200 if ( !className
.isEmpty() && editorFactory
)
202 return editorFactory
->createPart(
205 className
.toLatin1() );
211 KParts::Part
* PartController::createPart( const KUrl
& url
)
213 KMimeType::Ptr mimeType
;
215 //create a part for empty text file
216 mimeType
= KMimeType::mimeType("text/plain");
217 else if ( !url
.isValid() )
220 mimeType
= KMimeType::findByUrl( url
);
225 "KParts/ReadWritePart", "KParts/ReadOnlyPart"
228 QString classNames
[] =
230 "KParts::ReadWritePart", "KParts::ReadOnlyPart"
232 KParts::Factory
*editorFactory
= 0;
233 for ( uint i
= 0; i
< 2; ++i
)
235 editorFactory
= d
->findPartFactory( mimeType
->name(), services
[ i
] );
238 className
= classNames
[ i
];
243 if ( !className
.isEmpty() && editorFactory
)
245 KParts::Part
* part
= editorFactory
->createPart(
248 className
.toLatin1() );
251 readOnly( part
) ->openUrl( url
);
259 KParts::ReadOnlyPart
* PartController::activeReadOnly() const
261 return readOnly( activePart() );
264 KParts::ReadWritePart
* PartController::activeReadWrite() const
266 return readWrite( activePart() );
269 KParts::ReadOnlyPart
* PartController::readOnly( KParts::Part
* part
) const
271 return qobject_cast
<KParts::ReadOnlyPart
*>( part
);
274 KParts::ReadWritePart
* PartController::readWrite( KParts::Part
* part
) const
276 return qobject_cast
<KParts::ReadWritePart
*>( part
);
279 void PartController::loadSettings( bool projectIsLoaded
)
281 Q_UNUSED( projectIsLoaded
);
284 void PartController::saveSettings( bool projectIsLoaded
)
286 Q_UNUSED( projectIsLoaded
);
289 void PartController::initialize()
292 void PartController::cleanup()
296 #include "partcontroller.moc"