kmail

editorwatcher.cpp

00001 /*
00002     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "editorwatcher.h"
00020 
00021 #include <config.h>
00022 
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 #include <kopenwith.h>
00027 #include <kprocess.h>
00028 #include <kuserprofile.h>
00029 
00030 #include <qsocketnotifier.h>
00031 
00032 #include <cassert>
00033 
00034 // inotify stuff taken from kdelibs/kio/kio/kdirwatch.cpp
00035 #ifdef HAVE_SYS_INOTIFY
00036 #include <sys/ioctl.h>
00037 #include <sys/inotify.h>
00038 #include <fcntl.h>
00039 #elif HAVE_INOTIFY
00040 #include <sys/ioctl.h>
00041 #include <unistd.h>
00042 #include <fcntl.h>
00043 #include <sys/syscall.h>
00044 #include <sys/inotify.h>
00045 #endif
00046 
00047 using namespace KMail;
00048 
00049 EditorWatcher::EditorWatcher(const KURL & url, const QString &mimeType, bool openWith, QObject * parent) :
00050     QObject( parent ),
00051     mUrl( url ),
00052     mMimeType( mimeType ),
00053     mOpenWith( openWith ),
00054     mEditor( 0 ),
00055     mHaveInotify( false ),
00056     mFileOpen( false ),
00057     mEditorRunning( false ),
00058     mFileModified( true ), // assume the worst unless we know better
00059     mDone( false )
00060 {
00061   assert( mUrl.isLocalFile() );
00062   connect( &mTimer, SIGNAL(timeout()), SLOT(checkEditDone()) );
00063 }
00064 
00065 bool EditorWatcher::start()
00066 {
00067   // find an editor
00068   KURL::List list;
00069   list.append( mUrl );
00070   KService::Ptr offer = KServiceTypeProfile::preferredService( mMimeType, "Application" );
00071   if ( mOpenWith || !offer ) {
00072     KOpenWithDlg dlg( list, i18n("Edit with:"), QString::null, 0 );
00073     if ( !dlg.exec() )
00074       return false;
00075     offer = dlg.service();
00076     if ( !offer )
00077       return false;
00078   }
00079 
00080 #ifdef HAVE_INOTIFY
00081   // monitor file
00082   mInotifyFd = inotify_init();
00083   if ( mInotifyFd > 0 ) {
00084     mInotifyWatch = inotify_add_watch( mInotifyFd, mUrl.path().latin1(), IN_CLOSE | IN_OPEN | IN_MODIFY );
00085     if ( mInotifyWatch >= 0 ) {
00086       QSocketNotifier *sn = new QSocketNotifier( mInotifyFd, QSocketNotifier::Read, this );
00087       connect( sn, SIGNAL(activated(int)), SLOT(inotifyEvent()) );
00088       mHaveInotify = true;
00089       mFileModified = false;
00090     }
00091   } else {
00092     kdWarning(5006) << k_funcinfo << "Failed to activate INOTIFY!" << endl;
00093   }
00094 #endif
00095 
00096   // start the editor
00097   QStringList params = KRun::processDesktopExec( *offer, list, false );
00098   mEditor = new KProcess( this );
00099   *mEditor << params;
00100   connect( mEditor, SIGNAL(processExited(KProcess*)), SLOT(editorExited()) );
00101   if ( !mEditor->start() )
00102     return false;
00103   mEditorRunning = true;
00104 
00105   mEditTime.start();
00106   return true;
00107 }
00108 
00109 void EditorWatcher::inotifyEvent()
00110 {
00111   assert( mHaveInotify );
00112 #ifdef HAVE_INOTIFY
00113   int pending = -1;
00114   char buffer[4096];
00115   ioctl( mInotifyFd, FIONREAD, &pending );
00116   while ( pending > 0 ) {
00117     int size = read( mInotifyFd, buffer, QMIN( pending, (int)sizeof(buffer) ) );
00118     pending -= size;
00119     if ( size < 0 )
00120       break; // error
00121     int offset = 0;
00122     while ( size > 0 ) {
00123       struct inotify_event *event = (struct inotify_event *) &buffer[offset];
00124       size -= sizeof( struct inotify_event ) + event->len;
00125       offset += sizeof( struct inotify_event ) + event->len;
00126       if ( event->mask & IN_OPEN )
00127         mFileOpen = true;
00128       if ( event->mask & IN_CLOSE )
00129         mFileOpen = false;
00130       if ( event->mask & IN_MODIFY )
00131         mFileModified = true;
00132     }
00133   }
00134 #endif
00135   mTimer.start( 500, true );
00136 
00137 }
00138 
00139 void EditorWatcher::editorExited()
00140 {
00141   mEditorRunning = false;
00142   mTimer.start( 500, true );
00143 }
00144 
00145 void EditorWatcher::checkEditDone()
00146 {
00147   if ( mEditorRunning || (mFileOpen && mHaveInotify) || mDone )
00148     return;
00149   // protect us against double-deletion by calling this method again while
00150   // the subeventloop of the message box is running
00151   mDone = true;
00152   // nobody can edit that fast, we seem to be unable to detect
00153   // when the editor will be closed
00154   if ( mEditTime.elapsed() <= 3000 ) {
00155     KMessageBox::error( 0, i18n("KMail is unable to detect when the choosen editor is closed. "
00156          "To avoid data loss, editing the attachment will be aborted."), i18n("Unable to edit attachment") );
00157   }
00158 
00159   emit editDone( this );
00160   deleteLater();
00161 }
00162 
00163 #include "editorwatcher.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys