~ubuntu-branches/ubuntu/quantal/kdepim/quantal

« back to all changes in this revision

Viewing changes to calendarsupport/incidenceviewer.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:51 UTC
  • mto: This revision was merged to the branch mainline in revision 193.
  • Revision ID: package-import@ubuntu.com-20111215141751-bmhdpiwl23wd9w26
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company
3
 
 
4
 
  Author: Tobias Koenig <tokoe@kde.org>
5
 
 
6
 
  This library is free software; you can redistribute it and/or modify it
7
 
  under the terms of the GNU Library General Public License as published by
8
 
  the Free Software Foundation; either version 2 of the License, or (at your
9
 
  option) any later version.
10
 
 
11
 
  This library is distributed in the hope that it will be useful, but WITHOUT
12
 
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
14
 
  License for more details.
15
 
 
16
 
  You should have received a copy of the GNU Library General Public License
17
 
  along with this library; see the file COPYING.LIB.  If not, write to the
18
 
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 
  02110-1301, USA.
20
 
*/
21
 
 
22
 
#include "incidenceviewer.h"
23
 
#include "utils.h"
24
 
 
25
 
#include "libkdepimdbusinterfaces/urihandler.h"
26
 
#include "incidenceattachmentmodel.h"
27
 
 
28
 
#include <Akonadi/CollectionFetchJob>
29
 
#include <Akonadi/ItemFetchScope>
30
 
 
31
 
#include <KCalUtils/IncidenceFormatter>
32
 
 
33
 
#include <KJob>
34
 
#include <KSystemTimeZone>
35
 
#include <KTextBrowser>
36
 
 
37
 
#include <QVBoxLayout>
38
 
 
39
 
using namespace CalendarSupport;
40
 
 
41
 
class TextBrowser : public KTextBrowser
42
 
{
43
 
  public:
44
 
    TextBrowser( QWidget *parent = 0 )
45
 
      : KTextBrowser( parent )
46
 
    {
47
 
#ifdef KDEPIM_MOBILE_UI
48
 
      setFrameStyle( QFrame::NoFrame );
49
 
#endif
50
 
    }
51
 
 
52
 
    void setSource( const QUrl &name )
53
 
    {
54
 
      QString uri = name.toString();
55
 
      // QTextBrowser for some reason insists on putting // or / in links,
56
 
      // this is a crude workaround
57
 
      if ( uri.startsWith( QLatin1String( "uid:" ) ) ||
58
 
           uri.startsWith( QLatin1String( "kmail:" ) ) ||
59
 
           uri.startsWith( QString( "urn:x-ical" ).section( ':', 0, 0 ) ) ||
60
 
           uri.startsWith( QLatin1String( "news:" ) ) ||
61
 
           uri.startsWith( QLatin1String( "mailto:" ) ) ) {
62
 
        uri.replace( QRegExp( "^([^:]+:)/+" ), "\\1" );
63
 
      }
64
 
 
65
 
      UriHandler::process( uri );
66
 
    }
67
 
};
68
 
 
69
 
class IncidenceViewer::Private
70
 
{
71
 
  public:
72
 
    Private( IncidenceViewer *parent )
73
 
      : mParent( parent ), mDelayedClear( false ), mParentCollectionFetchJob( 0 ),
74
 
        mAttachmentModel( 0 )
75
 
    {
76
 
      mBrowser = new TextBrowser;
77
 
    }
78
 
 
79
 
    void updateView()
80
 
    {
81
 
      QString text;
82
 
 
83
 
      if ( mCurrentItem.isValid() ) {
84
 
        text = KCalUtils::IncidenceFormatter::extensiveDisplayStr(
85
 
          CalendarSupport::displayName( mParentCollection ),
86
 
          CalendarSupport::incidence( mCurrentItem ),
87
 
          mDate, KSystemTimeZones::local() );
88
 
        text.prepend( mHeaderText );
89
 
        mBrowser->setHtml( text );
90
 
      } else {
91
 
        text = mDefaultText;
92
 
        if ( !mDelayedClear ) {
93
 
          mBrowser->setHtml( text );
94
 
        }
95
 
      }
96
 
 
97
 
    }
98
 
 
99
 
    void slotParentCollectionFetched( KJob *job )
100
 
    {
101
 
      mParentCollectionFetchJob = 0;
102
 
      mParentCollection = Akonadi::Collection();
103
 
 
104
 
      if ( !job->error() ) {
105
 
        Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
106
 
        if ( !fetchJob->collections().isEmpty() ) {
107
 
          mParentCollection = fetchJob->collections().first();
108
 
        }
109
 
      }
110
 
 
111
 
      updateView();
112
 
    }
113
 
 
114
 
    IncidenceViewer *mParent;
115
 
    TextBrowser *mBrowser;
116
 
    Akonadi::Item mCurrentItem;
117
 
    QDate mDate;
118
 
    QString mHeaderText;
119
 
    QString mDefaultText;
120
 
    bool mDelayedClear;
121
 
    Akonadi::Collection mParentCollection;
122
 
    Akonadi::CollectionFetchJob *mParentCollectionFetchJob;
123
 
    IncidenceAttachmentModel *mAttachmentModel;
124
 
};
125
 
 
126
 
IncidenceViewer::IncidenceViewer( QWidget *parent )
127
 
  : QWidget( parent ), d( new Private( this ) )
128
 
{
129
 
  QVBoxLayout *layout = new QVBoxLayout( this );
130
 
  layout->setMargin( 0 );
131
 
 
132
 
  d->mBrowser->setNotifyClick( true );
133
 
  d->mBrowser->setMinimumHeight( 1 );
134
 
 
135
 
  layout->addWidget( d->mBrowser );
136
 
 
137
 
  // always fetch full payload for incidences
138
 
  fetchScope().fetchFullPayload();
139
 
  fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
140
 
 
141
 
  d->updateView();
142
 
}
143
 
 
144
 
IncidenceViewer::~IncidenceViewer()
145
 
{
146
 
  delete d;
147
 
}
148
 
 
149
 
Akonadi::Item IncidenceViewer::incidence() const
150
 
{
151
 
  return ItemMonitor::item();
152
 
}
153
 
 
154
 
QDate IncidenceViewer::activeDate() const
155
 
{
156
 
  return d->mDate;
157
 
}
158
 
 
159
 
QAbstractItemModel *IncidenceViewer::attachmentModel() const
160
 
{
161
 
  if ( !d->mAttachmentModel ) {
162
 
    d->mAttachmentModel =
163
 
      new IncidenceAttachmentModel( const_cast<IncidenceViewer*>( this ) );
164
 
  }
165
 
  return d->mAttachmentModel;
166
 
}
167
 
 
168
 
void IncidenceViewer::setDelayedClear( bool delayed )
169
 
{
170
 
  d->mDelayedClear = delayed;
171
 
}
172
 
 
173
 
void IncidenceViewer::setDefaultMessage( const QString &message )
174
 
{
175
 
  d->mDefaultText = message;
176
 
}
177
 
 
178
 
void IncidenceViewer::setHeaderText( const QString &text )
179
 
{
180
 
  d->mHeaderText = text;
181
 
}
182
 
 
183
 
void IncidenceViewer::setIncidence( const Akonadi::Item &incidence, const QDate &date )
184
 
{
185
 
  d->mDate = date;
186
 
  ItemMonitor::setItem( incidence );
187
 
 
188
 
  d->updateView();
189
 
}
190
 
 
191
 
void IncidenceViewer::itemChanged( const Akonadi::Item &item )
192
 
{
193
 
  if ( !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
194
 
    d->mBrowser->clear();
195
 
    return;
196
 
  }
197
 
 
198
 
  d->mCurrentItem = item;
199
 
 
200
 
  if ( d->mAttachmentModel ) {
201
 
    d->mAttachmentModel->setItem( d->mCurrentItem );
202
 
  }
203
 
 
204
 
  if ( d->mParentCollectionFetchJob ) {
205
 
    disconnect( d->mParentCollectionFetchJob, SIGNAL(result(KJob *)),
206
 
                this, SLOT(slotParentCollectionFetched(KJob *)) );
207
 
    delete d->mParentCollectionFetchJob;
208
 
  }
209
 
 
210
 
  d->mParentCollectionFetchJob =
211
 
    new Akonadi::CollectionFetchJob( d->mCurrentItem.parentCollection(),
212
 
                                     Akonadi::CollectionFetchJob::Base, this );
213
 
 
214
 
  connect( d->mParentCollectionFetchJob, SIGNAL(result(KJob *)),
215
 
           this, SLOT(slotParentCollectionFetched(KJob *)) );
216
 
}
217
 
 
218
 
void IncidenceViewer::itemRemoved()
219
 
{
220
 
  d->mBrowser->clear();
221
 
}
222
 
 
223
 
#include "incidenceviewer.moc"