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