• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • document
  • info
documentinfotool.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2008,2010,2012 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "documentinfotool.h"
24 
25 // lib
26 #include "bytearraymodeliodevice.h"
27 #include <bytearraydocument.h>
28 // Kasten core
29 #include <documentsyncmanager.h>
30 #include <abstractmodelsynchronizer.h>
31 // Okteta core
32 #include <abstractbytearraymodel.h>
33 // KDE
34 #include <KLocale>
35 #include <KUrl>
36 // Qt
37 #include <QtGui/QApplication>
38 #include <QtCore/QTimer>
39 
40 
41 namespace Kasten2
42 {
43 static const int mimeTypeUpdateTimeInterval = 500; // msec
44 
45 
46 DocumentInfoTool::DocumentInfoTool( DocumentSyncManager* syncManager )
47  : mDocument( 0 ),
48  mByteArrayModel( 0 ),
49  mSynchronizer( 0 ),
50  mDocumentSyncManager( syncManager ),
51  mMimeTypeUpdateTimer( new QTimer(this) ),
52  mMimeType( 0 )
53 {
54  setObjectName( QLatin1String( "DocumentInfo" ) );
55 
56  mMimeTypeUpdateTimer->setInterval( mimeTypeUpdateTimeInterval );
57  mMimeTypeUpdateTimer->setSingleShot( true );
58  connect( mMimeTypeUpdateTimer, SIGNAL(timeout()), SLOT(updateMimeType()) );
59 }
60 
61 //TODO: file or document or ...?
62 QString DocumentInfoTool::title() const { return i18nc("@title:window", "File Info"); }
63 QString DocumentInfoTool::documentTitle() const
64 {
65  return mDocument ? mDocument->title() : QString();
66 }
67 
68 QString DocumentInfoTool::location() const
69 {
70  QString result;
71  if( mDocument )
72  {
73  const KUrl url = mDocumentSyncManager->urlOf( mDocument );
74  result = url.isLocalFile() ? url.path() : url.prettyUrl();
75  }
76  return result;
77 }
78 
79 int DocumentInfoTool::documentSize() const
80 {
81  int documentSize = -1;
82  if( mByteArrayModel )
83  documentSize = mByteArrayModel->size();
84 
85  return documentSize;
86 }
87 
88 void DocumentInfoTool::setTargetModel( AbstractModel* model )
89 {
90  if( mSynchronizer ) mSynchronizer->disconnect( this );
91  if( mDocument ) mDocument->disconnect( this );
92  if( mByteArrayModel ) mByteArrayModel->disconnect( this );
93 
94  mDocument = model ? model->findBaseModel<ByteArrayDocument*>() : 0;
95  mByteArrayModel = mDocument ? mDocument->content() : 0;
96 
97  const bool hasDocument = ( mDocument != 0 );
98  AbstractModelSynchronizer* synchronizer = 0;
99  QString documentTitle;
100  int documentSize = -1;
101  if( hasDocument )
102  {
103  documentTitle = mDocument->title();
104  documentSize = mByteArrayModel->size();
105  synchronizer = mDocument->synchronizer();
106 
107  connect( mDocument, SIGNAL(titleChanged(QString)),
108  SIGNAL(documentTitleChanged(QString)) );
109  connect( mDocument, SIGNAL(synchronizerChanged(Kasten2::AbstractModelSynchronizer*)),
110  SLOT(onSynchronizerChanged(Kasten2::AbstractModelSynchronizer*)) );
111  connect( mByteArrayModel, SIGNAL(contentsChanged(Okteta::ArrayChangeMetricsList)),
112  SLOT(onContentsChanged()) );
113  }
114 
115  onSynchronizerChanged( synchronizer );
116 
117  emit documentTitleChanged( documentTitle );
118  emit documentSizeChanged( documentSize );
119 }
120 
121 // TODO: should this be done in a worker thread, to not block the UI?
122 void DocumentInfoTool::updateMimeType()
123 {
124  KMimeType::Ptr currentMimeType;
125 
126  if( mDocument )
127  {
128  // TODO: also get file mode, if available, for findByNameAndContent()
129  const QString filename = mDocumentSyncManager->urlOf( mDocument ).fileName();
130 
131  Okteta::ByteArrayModelIoDevice byteArrayModelIoDevice( mByteArrayModel );
132  currentMimeType = filename.isEmpty() ?
133  KMimeType::findByContent( &byteArrayModelIoDevice ) :
134  KMimeType::findByNameAndContent( filename, &byteArrayModelIoDevice );
135  }
136 
137 // TODO: KMimeType has no operator !=
138 // if( *mMimeType != *currentMimeType )
139  {
140  mMimeType = currentMimeType;
141  emit documentMimeTypeChanged( currentMimeType );
142  }
143 }
144 
145 
146 void DocumentInfoTool::onContentsChanged()
147 {
148  if( ! mMimeTypeUpdateTimer->isActive() )
149  mMimeTypeUpdateTimer->start();
150 
151  emit documentSizeChanged( mByteArrayModel->size() );
152 }
153 
154 void DocumentInfoTool::onSynchronizerChanged( AbstractModelSynchronizer* synchronizer )
155 {
156  // do an instant update, no need to delay
157  if( mMimeTypeUpdateTimer->isActive() )
158  mMimeTypeUpdateTimer->stop();
159  updateMimeType();
160 
161  if( mSynchronizer ) mSynchronizer->disconnect( this );
162  mSynchronizer = synchronizer;
163 
164  if( mSynchronizer )
165  {
166  connect( mSynchronizer, SIGNAL(urlChanged(KUrl)),
167  SLOT(onUrlChanged(KUrl)) );
168  }
169 
170  emit locationChanged( location() );
171 }
172 
173 void DocumentInfoTool::onUrlChanged( const KUrl& url )
174 {
175  Q_UNUSED( url );
176 
177  emit locationChanged( location() );
178 }
179 
180 DocumentInfoTool::~DocumentInfoTool() {}
181 
182 }
documentsyncmanager.h
Kasten2::mimeTypeUpdateTimeInterval
static const int mimeTypeUpdateTimeInterval
Definition: documentinfotool.cpp:43
Kasten2::DocumentInfoTool::setTargetModel
virtual void setTargetModel(AbstractModel *model)
Definition: documentinfotool.cpp:88
abstractbytearraymodel.h
Kasten2::DocumentInfoTool::title
virtual QString title() const
Definition: documentinfotool.cpp:62
Kasten2::DocumentInfoTool::location
QString location() const
Definition: documentinfotool.cpp:68
Kasten2::DocumentSyncManager::urlOf
KUrl urlOf(AbstractDocument *document) const
Definition: documentsyncmanager.cpp:78
Kasten2::DocumentInfoTool::documentTitleChanged
void documentTitleChanged(const QString &documentTitle)
Kasten2::DocumentInfoTool::documentTitle
QString documentTitle() const
Definition: documentinfotool.cpp:63
Kasten2::DocumentInfoTool::DocumentInfoTool
DocumentInfoTool(DocumentSyncManager *syncManager)
Definition: documentinfotool.cpp:46
Kasten2::DocumentInfoTool::locationChanged
void locationChanged(const QString &location)
Kasten2::DocumentInfoTool::~DocumentInfoTool
virtual ~DocumentInfoTool()
Definition: documentinfotool.cpp:180
Kasten2::ByteArrayDocument::title
virtual QString title() const
Definition: bytearraydocument.cpp:63
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Kasten2::DocumentInfoTool::documentMimeTypeChanged
void documentMimeTypeChanged(KMimeType::Ptr mimeType)
abstractmodelsynchronizer.h
Okteta::ArrayChangeMetricsList
Definition: arraychangemetricslist.h:36
bytearraymodeliodevice.h
Okteta::ByteArrayModelIoDevice
Definition: bytearraymodeliodevice.h:38
Kasten2::ByteArrayDocument::content
virtual Okteta::AbstractByteArrayModel * content() const
Definition: bytearraydocument.cpp:61
documentinfotool.h
Kasten2::AbstractTool::titleChanged
void titleChanged(const QString &newTitle)
Kasten2::AbstractModel::findBaseModel
T findBaseModel() const
returns the first baseModel which is of type T, or null if none is found.
Definition: abstractmodel.h:93
Kasten2::DocumentInfoTool::documentSizeChanged
void documentSizeChanged(int newSize)
Kasten2::AbstractModelSynchronizer
possible actions:
Definition: abstractmodelsynchronizer.h:61
Kasten2::ByteArrayDocument
Definition: bytearraydocument.h:54
Kasten2::AbstractModel
Definition: abstractmodel.h:40
Kasten2::DocumentInfoTool::documentSize
int documentSize() const
Definition: documentinfotool.cpp:79
bytearraydocument.h
Kasten2::AbstractDocument::synchronizer
AbstractModelSynchronizer * synchronizer() const
Definition: abstractdocument.cpp:40
Kasten2::DocumentSyncManager
Definition: documentsyncmanager.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal