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 <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 ),
00059 mDone( false )
00060 {
00061 assert( mUrl.isLocalFile() );
00062 connect( &mTimer, SIGNAL(timeout()), SLOT(checkEditDone()) );
00063 }
00064
00065 bool EditorWatcher::start()
00066 {
00067
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
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
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;
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
00150
00151 mDone = true;
00152
00153
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"