TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tdeioslavetest.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. This file is or will be part of KDE desktop environment
  3. Copyright 1999 Matt Koss <koss@miesto.sk>
  4. It is licensed under GPL version 2.
  5. If it is part of KDE libraries than this file is licensed under
  6. LGPL version 2.
  7. */
  8. #include <tqlayout.h>
  9. #include <tqmessagebox.h>
  10. #include <tqdir.h>
  11. #include <kacl.h>
  12. #include <tdeapplication.h>
  13. #include <tdecmdlineargs.h>
  14. #include <kdebug.h>
  15. #include <tdelocale.h>
  16. #include <kurl.h>
  17. #include <kstatusbar.h>
  18. #include <tdeio/job.h>
  19. #include <tdeio/scheduler.h>
  20. #include <kprotocolinfo.h>
  21. #include "tdeioslavetest.h"
  22. using namespace TDEIO;
  23. KioslaveTest::KioslaveTest( TQString src, TQString dest, uint op, uint pr )
  24. : TDEMainWindow(0, "")
  25. {
  26. job = 0L;
  27. main_widget = new TQWidget( this, "");
  28. TQBoxLayout *topLayout = new TQVBoxLayout( main_widget, 10, 5 );
  29. TQGridLayout *grid = new TQGridLayout( 2, 2, 10 );
  30. topLayout->addLayout( grid );
  31. grid->setRowStretch(0,1);
  32. grid->setRowStretch(1,1);
  33. grid->setColStretch(0,1);
  34. grid->setColStretch(1,100);
  35. lb_from = new TQLabel( "From:", main_widget );
  36. grid->addWidget( lb_from, 0, 0 );
  37. le_source = new TQLineEdit( main_widget );
  38. grid->addWidget( le_source, 0, 1 );
  39. le_source->setText( src );
  40. lb_to = new TQLabel( "To:", main_widget );
  41. grid->addWidget( lb_to, 1, 0 );
  42. le_dest = new TQLineEdit( main_widget );
  43. grid->addWidget( le_dest, 1, 1 );
  44. le_dest->setText( dest );
  45. // Operation groupbox & buttons
  46. opButtons = new TQButtonGroup( "Operation", main_widget );
  47. topLayout->addWidget( opButtons, 10 );
  48. connect( opButtons, TQT_SIGNAL(clicked(int)), TQT_SLOT(changeOperation(int)) );
  49. TQBoxLayout *hbLayout = new TQHBoxLayout( opButtons, 15 );
  50. rbList = new TQRadioButton( "List", opButtons );
  51. opButtons->insert( rbList, List );
  52. hbLayout->addWidget( rbList, 5 );
  53. rbListRecursive = new TQRadioButton( "ListRecursive", opButtons );
  54. opButtons->insert( rbListRecursive, ListRecursive );
  55. hbLayout->addWidget( rbListRecursive, 5 );
  56. rbStat = new TQRadioButton( "Stat", opButtons );
  57. opButtons->insert( rbStat, Stat );
  58. hbLayout->addWidget( rbStat, 5 );
  59. rbGet = new TQRadioButton( "Get", opButtons );
  60. opButtons->insert( rbGet, Get );
  61. hbLayout->addWidget( rbGet, 5 );
  62. rbPut = new TQRadioButton( "Put", opButtons );
  63. opButtons->insert( rbPut, Put );
  64. hbLayout->addWidget( rbPut, 5 );
  65. rbCopy = new TQRadioButton( "Copy", opButtons );
  66. opButtons->insert( rbCopy, Copy );
  67. hbLayout->addWidget( rbCopy, 5 );
  68. rbMove = new TQRadioButton( "Move", opButtons );
  69. opButtons->insert( rbMove, Move );
  70. hbLayout->addWidget( rbMove, 5 );
  71. rbDelete = new TQRadioButton( "Delete", opButtons );
  72. opButtons->insert( rbDelete, Delete );
  73. hbLayout->addWidget( rbDelete, 5 );
  74. rbShred = new TQRadioButton( "Shred", opButtons );
  75. opButtons->insert( rbShred, Shred );
  76. hbLayout->addWidget( rbShred, 5 );
  77. rbMkdir = new TQRadioButton( "Mkdir", opButtons );
  78. opButtons->insert( rbMkdir, Mkdir );
  79. hbLayout->addWidget( rbMkdir, 5 );
  80. rbMimetype = new TQRadioButton( "Mimetype", opButtons );
  81. opButtons->insert( rbMimetype, Mimetype );
  82. hbLayout->addWidget( rbMimetype, 5 );
  83. opButtons->setButton( op );
  84. changeOperation( op );
  85. // Progress groupbox & buttons
  86. progressButtons = new TQButtonGroup( "Progress dialog mode", main_widget );
  87. topLayout->addWidget( progressButtons, 10 );
  88. connect( progressButtons, TQT_SIGNAL(clicked(int)), TQT_SLOT(changeProgressMode(int)) );
  89. hbLayout = new TQHBoxLayout( progressButtons, 15 );
  90. rbProgressNone = new TQRadioButton( "None", progressButtons );
  91. progressButtons->insert( rbProgressNone, ProgressNone );
  92. hbLayout->addWidget( rbProgressNone, 5 );
  93. rbProgressDefault = new TQRadioButton( "Default", progressButtons );
  94. progressButtons->insert( rbProgressDefault, ProgressDefault );
  95. hbLayout->addWidget( rbProgressDefault, 5 );
  96. rbProgressStatus = new TQRadioButton( "Status", progressButtons );
  97. progressButtons->insert( rbProgressStatus, ProgressStatus );
  98. hbLayout->addWidget( rbProgressStatus, 5 );
  99. progressButtons->setButton( pr );
  100. changeProgressMode( pr );
  101. // statusbar progress widget
  102. statusProgress = new StatusbarProgress( statusBar() );
  103. statusBar()->addWidget( statusProgress, 0, true );
  104. // run & stop butons
  105. hbLayout = new TQHBoxLayout( topLayout, 15 );
  106. pbStart = new TQPushButton( "&Start", main_widget );
  107. pbStart->setFixedSize( pbStart->sizeHint() );
  108. connect( pbStart, TQT_SIGNAL(clicked()), TQT_SLOT(startJob()) );
  109. hbLayout->addWidget( pbStart, 5 );
  110. pbStop = new TQPushButton( "Sto&p", main_widget );
  111. pbStop->setFixedSize( pbStop->sizeHint() );
  112. pbStop->setEnabled( false );
  113. connect( pbStop, TQT_SIGNAL(clicked()), TQT_SLOT(stopJob()) );
  114. hbLayout->addWidget( pbStop, 5 );
  115. // close button
  116. close = new TQPushButton( "&Close", main_widget );
  117. close->setFixedSize( close->sizeHint() );
  118. connect(close, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotQuit()));
  119. topLayout->addWidget( close, 5 );
  120. main_widget->setMinimumSize( main_widget->sizeHint() );
  121. setCentralWidget( main_widget );
  122. slave = 0;
  123. // slave = TDEIO::Scheduler::getConnectedSlave(KURL("ftp://ftp.kde.org"));
  124. TDEIO::Scheduler::connect(TQT_SIGNAL(slaveConnected(TDEIO::Slave*)),
  125. this, TQT_SLOT(slotSlaveConnected()));
  126. TDEIO::Scheduler::connect(TQT_SIGNAL(slaveError(TDEIO::Slave*,int,const TQString&)),
  127. this, TQT_SLOT(slotSlaveError()));
  128. }
  129. void KioslaveTest::closeEvent( TQCloseEvent * ){
  130. slotQuit();
  131. }
  132. void KioslaveTest::slotQuit(){
  133. if ( job ) {
  134. job->kill( true ); // kill the job quietly
  135. }
  136. if (slave)
  137. TDEIO::Scheduler::disconnectSlave(slave);
  138. kapp->quit();
  139. }
  140. void KioslaveTest::changeOperation( int id ) {
  141. // only two urls for copy and move
  142. bool enab = rbCopy->isChecked() || rbMove->isChecked();
  143. le_dest->setEnabled( enab );
  144. selectedOperation = id;
  145. }
  146. void KioslaveTest::changeProgressMode( int id ) {
  147. progressMode = id;
  148. if ( progressMode == ProgressStatus ) {
  149. statusBar()->show();
  150. } else {
  151. statusBar()->hide();
  152. }
  153. }
  154. void KioslaveTest::startJob() {
  155. TQString sCurrent = TQDir::currentDirPath()+"/";
  156. KURL::encode_string(sCurrent);
  157. TQString sSrc( le_source->text() );
  158. KURL src( sCurrent, sSrc );
  159. if ( !src.isValid() ) {
  160. TQMessageBox::critical(this, "Kioslave Error Message", "Source URL is malformed" );
  161. return;
  162. }
  163. TQString sDest( le_dest->text() );
  164. KURL dest( sCurrent, sDest );
  165. if ( !dest.isValid() &&
  166. ( selectedOperation == Copy || selectedOperation == Move ) ) {
  167. TQMessageBox::critical(this, "Kioslave Error Message",
  168. "Destination URL is malformed" );
  169. return;
  170. }
  171. pbStart->setEnabled( false );
  172. bool observe = true;
  173. if (progressMode != ProgressDefault) {
  174. observe = false;
  175. }
  176. SimpleJob *myJob = 0;
  177. switch ( selectedOperation ) {
  178. case List:
  179. myJob = TDEIO::listDir( src );
  180. connect(myJob, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList&)),
  181. TQT_SLOT( slotEntries( TDEIO::Job*, const TDEIO::UDSEntryList&)));
  182. break;
  183. case ListRecursive:
  184. myJob = TDEIO::listRecursive( src );
  185. connect(myJob, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList&)),
  186. TQT_SLOT( slotEntries( TDEIO::Job*, const TDEIO::UDSEntryList&)));
  187. break;
  188. case Stat:
  189. myJob = TDEIO::stat( src, false, 2 );
  190. break;
  191. case Get:
  192. myJob = TDEIO::get( src, true );
  193. connect(myJob, TQT_SIGNAL( data( TDEIO::Job*, const TQByteArray &)),
  194. TQT_SLOT( slotData( TDEIO::Job*, const TQByteArray &)));
  195. break;
  196. case Put:
  197. putBuffer = 0;
  198. myJob = TDEIO::put( src, -1, true, false);
  199. connect(myJob, TQT_SIGNAL( dataReq( TDEIO::Job*, TQByteArray &)),
  200. TQT_SLOT( slotDataReq( TDEIO::Job*, TQByteArray &)));
  201. break;
  202. case Copy:
  203. job = TDEIO::copy( src, dest, observe );
  204. break;
  205. case Move:
  206. job = TDEIO::move( src, dest, observe );
  207. break;
  208. case Delete:
  209. job = TDEIO::del( src, false, observe );
  210. break;
  211. case Shred:
  212. job = TDEIO::del(src, true, observe);
  213. break;
  214. case Mkdir:
  215. myJob = TDEIO::mkdir( src );
  216. break;
  217. case Mimetype:
  218. myJob = TDEIO::mimetype( src );
  219. break;
  220. }
  221. if (myJob)
  222. {
  223. if (slave)
  224. TDEIO::Scheduler::assignJobToSlave(slave, myJob);
  225. job = myJob;
  226. }
  227. connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
  228. TQT_SLOT( slotResult( TDEIO::Job * ) ) );
  229. connect( job, TQT_SIGNAL( canceled( TDEIO::Job * ) ),
  230. TQT_SLOT( slotResult( TDEIO::Job * ) ) );
  231. if (progressMode == ProgressStatus) {
  232. statusProgress->setJob( job );
  233. }
  234. pbStop->setEnabled( true );
  235. }
  236. void KioslaveTest::slotResult( TDEIO::Job * _job )
  237. {
  238. if ( _job->error() )
  239. {
  240. _job->showErrorDialog();
  241. }
  242. else if ( selectedOperation == Stat )
  243. {
  244. UDSEntry entry = ((TDEIO::StatJob*)_job)->statResult();
  245. printUDSEntry( entry );
  246. }
  247. else if ( selectedOperation == Mimetype )
  248. {
  249. kdDebug() << "mimetype is " << ((TDEIO::MimetypeJob*)_job)->mimetype() << endl;
  250. }
  251. if (job == _job)
  252. job = 0L;
  253. pbStart->setEnabled( true );
  254. pbStop->setEnabled( false );
  255. }
  256. void KioslaveTest::slotSlaveConnected()
  257. {
  258. kdDebug() << "Slave connected." << endl;
  259. }
  260. void KioslaveTest::slotSlaveError()
  261. {
  262. kdDebug() << "Error connected." << endl;
  263. slave = 0;
  264. }
  265. static void printACL( const TQString& acl )
  266. {
  267. KACL kacl( acl );
  268. kdDebug() << "According to KACL: " << endl << kacl.asString() << endl;
  269. kdDebug() << "Owner: " << kacl.ownerPermissions() << endl;
  270. kdDebug() << "Owning group: " << kacl.owningGroupPermissions() << endl;
  271. kdDebug() << "Others: " << kacl.othersPermissions() << endl;
  272. }
  273. void KioslaveTest::printUDSEntry( const TDEIO::UDSEntry & entry )
  274. {
  275. TDEIO::UDSEntry::ConstIterator it = entry.begin();
  276. for( ; it != entry.end(); it++ ) {
  277. switch ((*it).m_uds) {
  278. case TDEIO::UDS_FILE_TYPE:
  279. kdDebug() << "File Type : " << (mode_t)((*it).m_long) << endl;
  280. if ( S_ISDIR( (mode_t)((*it).m_long) ) )
  281. {
  282. kdDebug() << "is a dir" << endl;
  283. }
  284. break;
  285. case TDEIO::UDS_ACCESS:
  286. kdDebug() << "Access permissions : " << (*it).m_long << endl;
  287. break;
  288. case TDEIO::UDS_EXTENDED_ACL:
  289. if( (*it).m_long == 1 )
  290. kdDebug() << "Has extended ACL information." << endl;
  291. break;
  292. case TDEIO::UDS_ACL_STRING:
  293. kdDebug() << "ACL: " << ( (*it).m_str.ascii() ) << endl;
  294. printACL( (*it).m_str );
  295. break;
  296. case TDEIO::UDS_DEFAULT_ACL_STRING:
  297. kdDebug() << "Default ACL: " << ( (*it).m_str.ascii() ) << endl;
  298. printACL( (*it).m_str );
  299. break;
  300. case TDEIO::UDS_USER:
  301. kdDebug() << "User : " << ((*it).m_str.ascii() ) << endl;
  302. break;
  303. case TDEIO::UDS_GROUP:
  304. kdDebug() << "Group : " << ((*it).m_str.ascii() ) << endl;
  305. break;
  306. case TDEIO::UDS_NAME:
  307. kdDebug() << "Name : " << ((*it).m_str.ascii() ) << endl;
  308. //m_strText = decodeFileName( (*it).m_str );
  309. break;
  310. case TDEIO::UDS_URL:
  311. kdDebug() << "URL : " << ((*it).m_str.ascii() ) << endl;
  312. break;
  313. case TDEIO::UDS_MIME_TYPE:
  314. kdDebug() << "MimeType : " << ((*it).m_str.ascii() ) << endl;
  315. break;
  316. case TDEIO::UDS_LINK_DEST:
  317. kdDebug() << "LinkDest : " << ((*it).m_str.ascii() ) << endl;
  318. break;
  319. case TDEIO::UDS_SIZE:
  320. kdDebug() << "Size: " << TDEIO::convertSize((*it).m_long) << endl;
  321. break;
  322. }
  323. }
  324. }
  325. void KioslaveTest::slotEntries(TDEIO::Job* job, const TDEIO::UDSEntryList& list) {
  326. KURL url = static_cast<TDEIO::ListJob*>( job )->url();
  327. KProtocolInfo::ExtraFieldList extraFields = KProtocolInfo::extraFields(url);
  328. UDSEntryListConstIterator it=list.begin();
  329. for (; it != list.end(); ++it) {
  330. // For each file...
  331. KProtocolInfo::ExtraFieldList::Iterator extraFieldsIt = extraFields.begin();
  332. UDSEntry::ConstIterator it2 = (*it).begin();
  333. for( ; it2 != (*it).end(); it2++ ) {
  334. if ((*it2).m_uds == UDS_NAME)
  335. kdDebug() << "" << ( *it2 ).m_str << endl;
  336. else if ( (*it2).m_uds == UDS_EXTRA) {
  337. Q_ASSERT( extraFieldsIt != extraFields.end() );
  338. TQString column = (*extraFieldsIt).name;
  339. //TQString type = (*extraFieldsIt).type;
  340. kdDebug() << " Extra data (" << column << ") :" << ( *it2 ).m_str << endl;
  341. ++extraFieldsIt;
  342. }
  343. }
  344. }
  345. }
  346. void KioslaveTest::slotData(TDEIO::Job*, const TQByteArray &data)
  347. {
  348. if (data.size() == 0)
  349. {
  350. kdDebug(0) << "Data: <End>" << endl;
  351. }
  352. else
  353. {
  354. kdDebug(0) << "Data: \"" << TQCString(data, data.size()+1) << "\"" << endl;
  355. }
  356. }
  357. void KioslaveTest::slotDataReq(TDEIO::Job*, TQByteArray &data)
  358. {
  359. const char *fileDataArray[] =
  360. {
  361. "Hello world\n",
  362. "This is a test file\n",
  363. "You can safely delete it.\n",
  364. "BIG\n",
  365. 0
  366. };
  367. const char *fileData = fileDataArray[putBuffer++];
  368. if (!fileData)
  369. {
  370. kdDebug(0) << "DataReq: <End>" << endl;
  371. return;
  372. }
  373. if (!strcmp(fileData, "BIG\n"))
  374. data.fill(0, 29*1024*1024);
  375. else
  376. data.duplicate(fileData, strlen(fileData));
  377. kdDebug(0) << "DataReq: \"" << fileData << "\"" << endl;
  378. }
  379. void KioslaveTest::stopJob() {
  380. kdDebug() << "KioslaveTest::stopJob()" << endl;
  381. job->kill();
  382. job = 0L;
  383. pbStop->setEnabled( false );
  384. pbStart->setEnabled( true );
  385. }
  386. static const char version[] = "v0.0.0 0000"; // :-)
  387. static const char description[] = "Test for tdeioslaves";
  388. static TDECmdLineOptions options[] =
  389. {
  390. { "s", 0, 0 },
  391. { "src <src>", "Source URL", "" },
  392. { "d", 0, 0 },
  393. { "dest <dest>", "Destination URL", "" },
  394. { "o", 0, 0 },
  395. { "operation <operation>", "Operation (list,listrecursive,stat,get,put,copy,move,del,shred,mkdir)", "copy" },
  396. { "p", 0, 0 },
  397. { "progress <progress>", "Progress Type (none,default,status)", "default" },
  398. TDECmdLineLastOption
  399. };
  400. int main(int argc, char **argv) {
  401. TDECmdLineArgs::init( argc, argv, "tdeioslavetest", description, version );
  402. TDECmdLineArgs::addCmdLineOptions( options );
  403. TDEApplication app;
  404. TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
  405. TQString src = args->getOption("src");
  406. TQString dest = args->getOption("dest");
  407. uint op = 0;
  408. uint pr = 0;
  409. TQString tmps;
  410. tmps = args->getOption("operation");
  411. if ( tmps == "list") {
  412. op = KioslaveTest::List;
  413. } else if ( tmps == "listrecursive") {
  414. op = KioslaveTest::ListRecursive;
  415. } else if ( tmps == "stat") {
  416. op = KioslaveTest::Stat;
  417. } else if ( tmps == "get") {
  418. op = KioslaveTest::Get;
  419. } else if ( tmps == "put") {
  420. op = KioslaveTest::Put;
  421. } else if ( tmps == "copy") {
  422. op = KioslaveTest::Copy;
  423. } else if ( tmps == "move") {
  424. op = KioslaveTest::Move;
  425. } else if ( tmps == "del") {
  426. op = KioslaveTest::Delete;
  427. } else if ( tmps == "shred") {
  428. op = KioslaveTest::Shred;
  429. } else if ( tmps == "mkdir") {
  430. op = KioslaveTest::Mkdir;
  431. } else TDECmdLineArgs::usage("unknown operation");
  432. tmps = args->getOption("progress");
  433. if ( tmps == "none") {
  434. pr = KioslaveTest::ProgressNone;
  435. } else if ( tmps == "default") {
  436. pr = KioslaveTest::ProgressDefault;
  437. } else if ( tmps == "status") {
  438. pr = KioslaveTest::ProgressStatus;
  439. } else TDECmdLineArgs::usage("unknown progress mode");
  440. args->clear(); // Free up memory
  441. KioslaveTest test( src, dest, op, pr );
  442. test.show();
  443. // Bug in KTMW / Qt / layouts ?
  444. test.resize( test.sizeHint() );
  445. app.setMainWidget(&test);
  446. app.exec();
  447. }
  448. #include "tdeioslavetest.moc"