Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Add support for mimetypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
pasnox committed Aug 12, 2012
1 parent a31fe7b commit 9a6ebcd
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
26 changes: 26 additions & 0 deletions qodeedit.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ QT *= xml
INCLUDEPATH *= $$getFolders( . )
DEPENDPATH *= $${INCLUDEPATH}

greaterThan( QT_MAJOR_VERSION, 4 ) {
} else:greaterThan( QT_MAJOR_VERSION, 3 ) {
MIMETYPES_QT4_ROOT = mimetypes-qt4.git

exists( $${MIMETYPES_QT4_ROOT} ) {
SOURCES_PATHS = $$getFolders( $${MIMETYPES_QT4_ROOT} )
DEPENDPATH *= $${SOURCES_PATHS}
INCLUDEPATH *= $${SOURCES_PATHS}

include( $${MIMETYPES_QT4_ROOT}/mimetypes/mimetypes.pri )

HEADERS *= $${MIMETYPES_QT4_ROOT}/io/qstandardpaths.h
SOURCES *= $${MIMETYPES_QT4_ROOT}/io/qstandardpaths.cpp

macx {
SOURCES *= $${MIMETYPES_QT4_ROOT}/io/*_mac.c*
} else:unix {
SOURCES *= $${MIMETYPES_QT4_ROOT}/io/*_unix.c*
} else:win32 {
SOURCES *= $${MIMETYPES_QT4_ROOT}/io/*_win.c*
}
} else {
error( Qt 4 build need dependency project mimetypes-qt4 uncompressed in current folder as $${MIMETYPES_QT4_ROOT}. You can get it here https://github.com/pasnox/mimetypes-qt4 )
}
}

FORMS *= \
src/example/UIMain.ui

Expand Down
20 changes: 20 additions & 0 deletions src/example/UIMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "SyntaxHighlighter.h"
#include "TextDocument.h"

#include <QUrl>

UIMain* UIMain::qMain = 0;

// QodeEditor
Expand Down Expand Up @@ -135,6 +137,24 @@ UIMain::UIMain( QWidget* parent )
}

statusBar()->showMessage( error );

/*qWarning() << Syntax::Factory::mimeTypeForFile( "toto.h" );
qWarning() << Syntax::Factory::mimeTypeForFile( "toto.c" );
qWarning() << Syntax::Factory::mimeTypeForFile( "toto.cpp" );
qWarning() << Syntax::Factory::mimeTypeForFile( "toto.adb" );
qWarning() << Syntax::Factory::mimeTypeForData( "#include <iostream>\n" );
qWarning() << Syntax::Factory::mimeTypeForData( "#include <stdlib.h>\n" );
qWarning() << Syntax::Factory::mimeTypeForData( "#import <stdlib.h>\n" );
qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.html" ) );
qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.pdf" ) );
qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.jpg" ) );
qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.h" );
qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.c" );
qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.cpp" );
qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.adb" );*/
}

UIMain::~UIMain()
Expand Down
68 changes: 68 additions & 0 deletions src/syntax/SyntaxFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#include <QDir>
#include <QPointer>
#include <QTime>
#include <QMimeDatabase>
#include <QDebug>

namespace Syntax {
namespace Factory {
QHash<QString, Syntax::Document> mDocuments;
QList<QPointer<Syntax::Model> > mModels;
QMimeDatabase mMimeDatabase;

// forward declare
void buildDocument( Syntax::Document& document );
Expand Down Expand Up @@ -227,6 +229,53 @@ QStringList Syntax::Factory::availableSyntaxes()
return syntaxes;
}

QString Syntax::Factory::mimeTypeForFile( const QString& fileName )
{
return mMimeDatabase.mimeTypeForFile( fileName ).name();
}

QString Syntax::Factory::mimeTypeForFile( const QFileInfo& fileInfo )
{
return mMimeDatabase.mimeTypeForFile( fileInfo ).name();
}

QString Syntax::Factory::mimeTypeForData( const QByteArray& data )
{
return mMimeDatabase.mimeTypeForData( data ).name();
}

QString Syntax::Factory::mimeTypeForData( QIODevice* device )
{
return mMimeDatabase.mimeTypeForData( device ).name();
}

QString Syntax::Factory::mimeTypeForFileNameAndData( const QString& fileName, QIODevice* device )
{
return mMimeDatabase.mimeTypeForFileNameAndData( fileName, device ).name();
}

QString Syntax::Factory::mimeTypeForFileNameAndData( const QString& fileName, const QByteArray& data )
{
return mMimeDatabase.mimeTypeForFileNameAndData( fileName, data ).name();
}

QString Syntax::Factory::mimeTypeForUrl( const QUrl& url )
{
return mMimeDatabase.mimeTypeForUrl( url ).name();
}

QStringList Syntax::Factory::mimeTypesForFileName( const QString& fileName )
{
const QList<QMimeType> mimeTypes = mMimeDatabase.mimeTypesForFileName( fileName );
QStringList names;

for ( int i = 0; i < mimeTypes.count(); i++ ) {
names << mimeTypes[ i ].name();
}

return names;
}

Syntax::Highlighter* Syntax::Factory::highlighter( const QString& name, TextDocument* textDocument )
{
return new Syntax::Highlighter( Syntax::Factory::document( name ), textDocument );
Expand All @@ -251,6 +300,25 @@ Syntax::Highlighter* Syntax::Factory::highlighterForFilePath( const QString& fil
return Syntax::Factory::highlighter( ( documents.end() -1 ).value()->name(), textDocument );
}

Syntax::Highlighter* Syntax::Factory::highlighterForMimeType( const QString& mimeType, TextDocument* textDocument )
{
QMap<int, Syntax::Document*> documents;

foreach ( const QString& name, Syntax::Factory::mDocuments.keys() ) {
Syntax::Document& document = Syntax::Factory::mDocuments[ name ];

if ( document.mimeTypes().contains( mimeType ) ) {
documents[ document.priority() ] = &document;
}
}

if ( documents.isEmpty() ) {
return 0;
}

return Syntax::Factory::highlighter( ( documents.end() -1 ).value()->name(), textDocument );
}

Syntax::Model* Syntax::Factory::model( QObject* parent )
{
Syntax::Factory::mModels << new Syntax::Model( &Syntax::Factory::mDocuments, parent );
Expand Down
14 changes: 14 additions & 0 deletions src/syntax/SyntaxFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <QString>

class QStringList;
class QFileInfo;
class QUrl;

class TextDocument;

Expand All @@ -19,8 +21,20 @@ namespace Factory {
void free();

QStringList availableSyntaxes();

QString mimeTypeForFile( const QString& fileName );
QString mimeTypeForFile( const QFileInfo& fileInfo );
QString mimeTypeForData( const QByteArray& data );
QString mimeTypeForData( QIODevice* device );
QString mimeTypeForFileNameAndData( const QString& fileName, QIODevice* device );
QString mimeTypeForFileNameAndData( const QString& fileName, const QByteArray& data );
QString mimeTypeForUrl( const QUrl& url );
QStringList mimeTypesForFileName( const QString& fileName );

Syntax::Highlighter* highlighter( const QString& name, TextDocument* textDocument = 0 );
Syntax::Highlighter* highlighterForFilePath( const QString& filePath, TextDocument* textDocument = 0 );
Syntax::Highlighter* highlighterForMimeType( const QString& mimeType, TextDocument* textDocument = 0 );

Syntax::Model* model( QObject* parent = 0 );
}; // Factory

Expand Down

0 comments on commit 9a6ebcd

Please sign in to comment.