Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / shell / partcontroller.cpp
blob575e83b51758eafcfe7772bc997b1a525dd1b71b
1 /***************************************************************************
2 * Copyright 2006 Adam Treat <treat@kde.org> *
3 * Copyright 2007 Alexander Dymo <adymo@kdevelop.org> *
4 * *
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. *
9 * *
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. *
14 * *
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"
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
26 #include <QFile>
27 #include <QTimer>
28 #include <QMutexLocker>
30 #include <kdebug.h>
31 #include <klocale.h>
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>
46 #include "core.h"
47 // #include "kdevmainwindow.h"
48 // #include "kdevconfig.h"
50 namespace KDevelop
53 class PartControllerPrivate
55 public:
56 PartControllerPrivate(): m_textEditor(0) {}
58 QString m_editor;
59 QStringList m_textTypes;
61 Core *m_core;
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(
71 mimeType,
72 "KParts/ReadOnlyPart",
73 QString( "'%1' in ServiceTypes" ).arg( partType ) );
75 if ( offers.count() > 0 )
77 KService::Ptr ptr;
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 )
86 ptr = ( *it );
90 // else we just take the first in the list
91 if ( !ptr )
93 ptr = offers.first();
95 KParts::Factory *factory = static_cast<KParts::Factory*>(
96 KLibLoader::self() ->factory(
97 QFile::encodeName( ptr->library() ) ) );
98 return factory;
101 return 0;
106 PartController::PartController(Core *core, QWidget *toplevel)
107 : KParts::PartManager( toplevel, 0 ), d(new PartControllerPrivate)
110 d->m_core = core;
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()
123 delete d;
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() ) )
132 isTextType = true;
135 bool isKDEText = false;
136 QVariant v = mimeType->property( "X-KDE-text" );
137 if ( v.isValid() )
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(
152 "text/plain",
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 );
167 return doc;
170 void PartController::removePart( KParts::Part *part )
172 #if 0
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 );
180 return ;
183 kWarning() << "Deleting text editor" << doc << "which does not have a smart interface." ;
185 #endif
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(
196 mimeType,
197 partType,
198 preferredName );
200 if ( !className.isEmpty() && editorFactory )
202 return editorFactory->createPart(
204 this,
205 className.toLatin1() );
208 return 0;
211 KParts::Part* PartController::createPart( const KUrl & url )
213 KMimeType::Ptr mimeType;
214 if ( url.isEmpty() )
215 //create a part for empty text file
216 mimeType = KMimeType::mimeType("text/plain");
217 else if ( !url.isValid() )
218 return 0;
219 else
220 mimeType = KMimeType::findByUrl( url );
222 QString className;
223 QString services[] =
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 ] );
236 if ( editorFactory )
238 className = classNames[ i ];
239 break;
243 if ( !className.isEmpty() && editorFactory )
245 KParts::Part * part = editorFactory->createPart(
247 this,
248 className.toLatin1() );
249 if( part )
251 readOnly( part ) ->openUrl( url );
252 return part;
256 return 0;
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"