00001 #include "onotifydemo.h"
00002
00003
00004 #include <opie2/odebug.h>
00005 #include <opie2/oapplication.h>
00006 #include <opie2/ofiledialog.h>
00007 #include <opie2/olistview.h>
00008 #include <opie2/ofilenotify.h>
00009 using namespace Opie::Core;
00010 using namespace Opie::Ui;
00011
00012
00013 #include <qcheckbox.h>
00014 #include <qfileinfo.h>
00015 #include <qvbox.h>
00016 #include <qhbox.h>
00017 #include <qhbuttongroup.h>
00018 #include <qvbuttongroup.h>
00019 #include <qmessagebox.h>
00020 #include <qpushbutton.h>
00021
00022 DemoApp::DemoApp( int argc, char** argv ) : OApplication( argc, argv, "libopie2 notify demo" )
00023 {
00024
00025 QVBox* vbox = new QVBox();
00026 setMainWidget( vbox );
00027
00028 l = new OListView( vbox );
00029 l->addColumn( "Notification Path" );
00030 l->addColumn( "Trigger Type" );
00031 l->addColumn( "Trigger Mask" );
00032 l->setColumnAlignment( 1, AlignCenter );
00033 l->setColumnAlignment( 2, AlignCenter );
00034
00035 QHBox* hbox = new QHBox( vbox );
00036 g2 = new QVButtonGroup( "Specify Trigger Type", hbox );
00037 QCheckBox* c1 = new QCheckBox( "Access", g2 );
00038 QCheckBox* c2 = new QCheckBox( "Modify", g2 );
00039 QCheckBox* c3 = new QCheckBox( "Attrib", g2 );
00040 QCheckBox* c4 = new QCheckBox( "CloseWrite", g2 );
00041 QCheckBox* c5 = new QCheckBox( "CloseNoWrite", g2 );
00042 QCheckBox* c6 = new QCheckBox( "MovedFrom", g2 );
00043 QCheckBox* c7 = new QCheckBox( "MovedTo", g2 );
00044 QCheckBox* c8 = new QCheckBox( "DeleteSubdir", g2 );
00045 QCheckBox* c9 = new QCheckBox( "DeleteFile", g2 );
00046 QCheckBox* c10 = new QCheckBox( "CreateSubdir", g2 );
00047 QCheckBox* c11 = new QCheckBox( "CreateFile", g2 );
00048 QCheckBox* c12 = new QCheckBox( "Unmount", g2 );
00049 g2->insert( c1, Access );
00050 g2->insert( c2, Modify );
00051 g2->insert( c3, Attrib );
00052 g2->insert( c4, CloseWrite );
00053 g2->insert( c5, CloseNoWrite );
00054 g2->insert( c6, MovedFrom );
00055 g2->insert( c7, MovedTo );
00056 g2->insert( c8, DeleteSubdir );
00057 g2->insert( c9, DeleteFile );
00058 g2->insert( c10, CreateSubdir );
00059 g2->insert( c11, CreateFile );
00060 g2->insert( c12, Unmount );
00061 connect( g2, SIGNAL( pressed(int) ), this, SLOT( modifierClicked(int) ) );
00062
00063 g1 = new QVButtonGroup( "Add/Remove", hbox );
00064 QPushButton* plus1 = new QPushButton( "Add\n&Single", g1 );
00065 QPushButton* plus2 = new QPushButton( "Add\n&Multi", g1 );
00066 QPushButton* minus = new QPushButton( "&Remove\nIt!", g1 );
00067 g1->insert( plus1, 0 );
00068 g1->insert( plus2, 1 );
00069 g1->insert( minus, 2 );
00070 connect( plus1, SIGNAL( clicked() ), this, SLOT( addSingle() ) );
00071 connect( plus2, SIGNAL( clicked() ), this, SLOT( addMulti() ) );
00072 connect( minus, SIGNAL( clicked() ), this, SLOT( delTrigger() ) );
00073
00074 g1->show();
00075 g2->show();
00076 l->show();
00077 hbox->show();
00078 vbox->show();
00079 showMainWidget( vbox );
00080 }
00081
00082 void DemoApp::addTrigger( bool multi )
00083 {
00084 if ( !m )
00085 {
00086 QMessageBox::warning( 0, "Add Trigger", "<p>Can't add trigger without at least one selected trigger type</p>", "&Sorry", 0 );
00087 return;
00088 }
00089
00090 QString filename = OFileDialog::getOpenFileName( OFileSelector::ExtendedAll );
00091 if ( !filename.isEmpty() )
00092 {
00093 bool success = true;
00094 odebug << "Filename = " << filename << oendl;
00095
00096 int fntype = m;
00097 QString modifier = QString().sprintf( " = 0x%08x", fntype );
00098
00099 if ( QFileInfo( filename ).isFile() )
00100 {
00101 if ( !multi )
00102 {
00103 success = OFileNotification::singleShot( filename, this, SLOT(unnamedTrigger()), (OFileNotificationType) fntype );
00104 }
00105 else
00106 {
00107 OFileNotification* fn = new OFileNotification();
00108 success = fn->watch( filename, false, (OFileNotificationType) fntype );
00109 connect( fn, SIGNAL(triggered(const QString&,unsigned int,const QString&)),
00110 this, SLOT(namedTrigger(const QString&,unsigned int,const QString&)) );
00111 }
00112 }
00113 else if ( QFileInfo( filename ).isDir() )
00114 {
00115 ODirNotification* dn = new ODirNotification();
00116 success = dn->watch( filename, !multi, (OFileNotificationType) fntype );
00117 connect( dn, SIGNAL(triggered(const QString&,unsigned int,const QString&)),
00118 this, SLOT(namedTrigger(const QString&,unsigned int,const QString&)) );
00119 }
00120 else
00121 {
00122 odebug << "Huh!? Neither file nor directory..." << oendl;
00123 return;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 {
00133 new OListViewItem( l, filename, multi ? "MULTI" : "SINGLE", modifier );
00134 }
00135 return;
00136 }
00137 else
00138 {
00139 odebug << "cancelled." << oendl;
00140 }
00141 }
00142
00143 void DemoApp::modifierClicked( int modifier ) { m = static_cast<OFileNotificationType>( (int)m ^ int(modifier) ); };
00144 void DemoApp::addSingle() { addTrigger(); };
00145 void DemoApp::addMulti() { addTrigger( true ); };
00146
00147 void DemoApp::delTrigger()
00148 {
00149 QListViewItem* item = l->selectedItem();
00150 if ( !item )
00151 {
00152 QMessageBox::warning( 0, "Del Trigger", "<p>No trigger selected!</p>", "&Sorry", 0 );
00153 return;
00154 }
00155 else
00156 {
00157 QString filename( item->text( 0 ) );
00158 odebug << "Filename = " << filename << oendl;
00159 }
00160 }
00161
00162 void DemoApp::unnamedTrigger()
00163 {
00164 owarn << "DemoApp::singleShotStrigger() : F I R E !!!!!" << oendl;
00165 }
00166
00167 void DemoApp::namedTrigger( const QString& path, unsigned int type, const QString& name )
00168 {
00169 owarn << "DemoApp::named trigger = ( " << path << ", " << type << ", " << name << " ) : F I R E !!!!!" << oendl;
00170 }
00171
00172 int main( int argc, char** argv )
00173 {
00174 DemoApp* app = new DemoApp( argc, argv );
00175 app->exec();
00176
00177 return 0;
00178
00179 }
00180