Commit 606ebaa8 authored by Laurent Montel's avatar Laurent Montel 😁

Make editorwatcher works. Fix attachmentedit work too

parent 58747988
......@@ -78,7 +78,7 @@ void AttachmentEditJob::setMainWindow(QWidget *mainWindow)
void AttachmentEditJob::slotAttachmentEditDone( MessageViewer::EditorWatcher* editorWatcher )
{
const QString name = editorWatcher->url().fileName();
const QString name = editorWatcher->url().path();
if ( editorWatcher->fileChanged() ) {
QFile file( name );
if ( file.open( QIODevice::ReadOnly ) ) {
......
......@@ -62,6 +62,13 @@ EditorWatcher::EditorWatcher( const KUrl & url, const QString &mimeType, OpenWit
connect( &mTimer, SIGNAL(timeout()), SLOT(checkEditDone()) );
}
EditorWatcher::~EditorWatcher()
{
#ifdef HAVE_SYS_INOTIFY_H
::close( mInotifyFd );
#endif
}
bool EditorWatcher::start()
{
// find an editor
......@@ -83,7 +90,9 @@ bool EditorWatcher::start()
// monitor file
mInotifyFd = inotify_init();
if ( mInotifyFd > 0 ) {
mInotifyWatch = inotify_add_watch( mInotifyFd, mUrl.path().toLatin1(), IN_CLOSE | IN_OPEN | IN_MODIFY );
(void)fcntl(mInotifyFd, F_SETFD, FD_CLOEXEC);
qDebug()<<" mUrl.path().toLatin1()"<<mUrl.path().toLatin1();
mInotifyWatch = inotify_add_watch( mInotifyFd, mUrl.path().toLatin1(), IN_CLOSE | IN_OPEN | IN_MODIFY | IN_ATTRIB );
if ( mInotifyWatch >= 0 ) {
QSocketNotifier *sn = new QSocketNotifier( mInotifyFd, QSocketNotifier::Read, this );
connect( sn, SIGNAL(activated(int)), SLOT(inotifyEvent()) );
......@@ -122,38 +131,53 @@ KUrl EditorWatcher::url() const
void EditorWatcher::inotifyEvent()
{
qDebug()<<" void EditorWatcher::inotifyEvent()";
assert( mHaveInotify );
#ifdef HAVE_SYS_INOTIFY_H
int pending = -1;
char buffer[4096];
int offsetStartRead = 0; // where we read into buffer
char buf[8192];
assert( mInotifyFd > -1 );
ioctl( mInotifyFd, FIONREAD, &pending );
while ( pending > 0 ) {
int size = read( mInotifyFd, buffer, qMin( pending, (int)sizeof(buffer) ) );
pending -= size;
if ( size < 0 )
break; // error
int offset = 0;
while ( size > 0 ) {
struct inotify_event *event = (struct inotify_event *) &buffer[offset];
qDebug()<<" size > 0"<<event->mask;
size -= sizeof( struct inotify_event ) + event->len;
offset += sizeof( struct inotify_event ) + event->len;
const int bytesToRead = qMin( pending, (int)sizeof( buf ) - offsetStartRead );
int bytesAvailable = read( mInotifyFd, &buf[offsetStartRead], bytesToRead );
pending -= bytesAvailable;
bytesAvailable += offsetStartRead;
offsetStartRead = 0;
int offsetCurrent = 0;
while ( bytesAvailable >= (int)sizeof( struct inotify_event ) ) {
const struct inotify_event * const event = (struct inotify_event *) &buf[offsetCurrent];
const int eventSize = sizeof( struct inotify_event ) + event->len;
if ( bytesAvailable < eventSize ) {
break;
}
bytesAvailable -= eventSize;
offsetCurrent += eventSize;
if ( event->mask & IN_OPEN ) {
mFileOpen = true;
qDebug()<<" IN OPEN";
}
if ( event->mask & IN_CLOSE ) {
mFileOpen = false;
qDebug()<<" IN CLOSE";
}
if ( event->mask & IN_MODIFY ) {
if ( event->mask & (IN_MODIFY|IN_ATTRIB) ) {
mFileModified = true;
qDebug()<<" IN MODIFY";
}
}
if (bytesAvailable > 0) {
// copy partial event to beginning of buffer
memmove(buf, &buf[offsetCurrent], bytesAvailable);
offsetStartRead = bytesAvailable;
}
}
#endif
mTimer.start( 500 );
}
......
......@@ -53,7 +53,7 @@ public:
*/
EditorWatcher( const KUrl &url, const QString &mimeType, OpenWithOption option,
QObject *parent, QWidget *parentWidget );
~EditorWatcher();
bool start();
bool fileChanged() const;
KUrl url() const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment