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_INOTIFY
00036 #include <sys/ioctl.h>
00037 #include <unistd.h>
00038 #include <fcntl.h>
00039 #include <sys/syscall.h>
00040 #include <linux/types.h>
00041 // Linux kernel headers are documented to not compile
00042 #define _S390_BITOPS_H
00043 #include <linux/inotify.h>
00044 
00045 static inline int inotify_init (void)
00046 {
00047   return syscall (__NR_inotify_init);
00048 }
00049 
00050 static inline int inotify_add_watch (int fd, const char *name, __u32 mask)
00051 {
00052   return syscall (__NR_inotify_add_watch, fd, name, mask);
00053 }
00054 
00055 static inline int inotify_rm_watch (int fd, __u32 wd)
00056 {
00057   return syscall (__NR_inotify_rm_watch, fd, wd);
00058 }
00059 #endif
00060 
00061 using namespace KMail;
00062 
00063 EditorWatcher::EditorWatcher(const KURL & url, const QString &mimeType, bool openWith, QObject * parent) :
00064     QObject( parent ),
00065     mUrl( url ),
00066     mMimeType( mimeType ),
00067     mOpenWith( openWith ),
00068     mEditor( 0 ),
00069     mHaveInotify( false ),
00070     mFileOpen( false ),
00071     mEditorRunning( false ),
00072     mFileModified( true ), // assume the worst unless we know better
00073     mDone( false )
00074 {
00075   assert( mUrl.isLocalFile() );
00076   connect( &mTimer, SIGNAL(timeout()), SLOT(checkEditDone()) );
00077 }
00078 
00079 bool EditorWatcher::start()
00080 {
00081   // find an editor
00082   KURL::List list;
00083   list.append( mUrl );
00084   KService::Ptr offer = KServiceTypeProfile::preferredService( mMimeType, "Application" );
00085   if ( mOpenWith || !offer ) {
00086     KOpenWithDlg dlg( list, i18n("Edit with:"), QString::null, 0 );
00087     if ( !dlg.exec() )
00088       return false;
00089     offer = dlg.service();
00090     if ( !offer )
00091       return false;
00092   }
00093 
00094 #ifdef HAVE_INOTIFY
00095   // monitor file
00096   mInotifyFd = inotify_init();
00097   if ( mInotifyFd > 0 ) {
00098     mInotifyWatch = inotify_add_watch( mInotifyFd, mUrl.path().latin1(), IN_CLOSE | IN_OPEN | IN_MODIFY );
00099     if ( mInotifyWatch >= 0 ) {
00100       QSocketNotifier *sn = new QSocketNotifier( mInotifyFd, QSocketNotifier::Read, this );
00101       connect( sn, SIGNAL(activated(int)), SLOT(inotifyEvent()) );
00102       mHaveInotify = true;
00103       mFileModified = false;
00104     }
00105   } else {
00106     kdWarning(5006) << k_funcinfo << "Failed to activate INOTIFY!" << endl;
00107   }
00108 #endif
00109 
00110   // start the editor
00111   QStringList params = KRun::processDesktopExec( *offer, list, false );
00112   mEditor = new KProcess( this );
00113   *mEditor << params;
00114   connect( mEditor, SIGNAL(processExited(KProcess*)), SLOT(editorExited()) );
00115   if ( !mEditor->start() )
00116     return false;
00117   mEditorRunning = true;
00118 
00119   mEditTime.start();
00120   return true;
00121 }
00122 
00123 void EditorWatcher::inotifyEvent()
00124 {
00125   assert( mHaveInotify );
00126 #ifdef HAVE_INOTIFY
00127   int pending = -1;
00128   char buffer[4096];
00129   ioctl( mInotifyFd, FIONREAD, &pending );
00130   while ( pending > 0 ) {
00131     int size = read( mInotifyFd, buffer, QMIN( pending, (int)sizeof(buffer) ) );
00132     pending -= size;
00133     if ( size < 0 )
00134       break; // error
00135     int offset = 0;
00136     while ( size > 0 ) {
00137       struct inotify_event *event = (struct inotify_event *) &buffer[offset];
00138       size -= sizeof( struct inotify_event ) + event->len;
00139       offset += sizeof( struct inotify_event ) + event->len;
00140       if ( event->mask & IN_OPEN )
00141         mFileOpen = true;
00142       if ( event->mask & IN_CLOSE )
00143         mFileOpen = false;
00144       if ( event->mask & IN_MODIFY )
00145         mFileModified = true;
00146     }
00147   }
00148 #endif
00149   mTimer.start( 500, true );
00150 
00151 }
00152 
00153 void EditorWatcher::editorExited()
00154 {
00155   mEditorRunning = false;
00156   mTimer.start( 500, true );
00157 }
00158 
00159 void EditorWatcher::checkEditDone()
00160 {
00161   if ( mEditorRunning || (mFileOpen && mHaveInotify) || mDone )
00162     return;
00163   // protect us against double-deletion by calling this method again while
00164   // the subeventloop of the message box is running
00165   mDone = true;
00166   // nobody can edit that fast, we seem to be unable to detect
00167   // when the editor will be closed
00168   if ( mEditTime.elapsed() <= 3000 ) {
00169     KMessageBox::error( 0, i18n("KMail is unable to detect when the choosen editor is closed. "
00170          "To avoid data loss, editing the attachment will be aborted."), i18n("Unable to edit attachment") );
00171   }
00172 
00173   emit editDone( this );
00174   deleteLater();
00175 }
00176 
00177 #include "editorwatcher.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys