Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.64 KB | None | 0 0
  1. /******************************************************************************
  2. * This file is part of the Gluon Development Platform
  3. * Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
  4. David Faure <faure@kde.org>
  5. * Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
  6. * Copyright (C) 2011 Laszlo Papp <lpapp@kde.org>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22.  
  23. #ifndef GLUONPLAYER_ABSTRACTJOB_H
  24. #define GLUONPLAYER_ABSTRACTJOB_H
  25.  
  26. #include <QtCore/QObject>
  27. #include <QtCore/QPair>
  28.  
  29. class AbstractJobPrivate;
  30. /**
  31. * The base class for all Gluon Player jobs.
  32. * For all jobs created in an application, the code looks like
  33. *
  34. * \code
  35. * void SomeClass::methodWithAsynchronousJobCall()
  36. * {
  37. * AbstractJob * job = someoperation( some parameters );
  38. * connect( job, SIGNAL( result( AbstractJob * ) ),
  39. * this, SLOT( handleResult( KJob * ) ) );
  40. * job->start();
  41. * }
  42. * \endcode
  43. * (other connects, specific to the job)
  44. *
  45. * And handleResult is usually at least:
  46. *
  47. * \code
  48. * void SomeClass::handleResult( KJob *job )
  49. * {
  50. * if ( job->error() )
  51. * doSomething();
  52. * }
  53. * \endcode
  54. *
  55. * With the synchronous interface the code looks like
  56. *
  57. * \code
  58. * void SomeClass::methodWithSynchronousJobCall()
  59. * {
  60. * AbstractJob *job = someoperation( some parameters );
  61. * if ( !job->exec() )
  62. * {
  63. * // An error occurred
  64. * }
  65. * else
  66. * {
  67. * // Do something
  68. * }
  69. * }
  70. * \endcode
  71. *
  72. * @note: AbstractJob and its subclasses is meant to be used
  73. * in a fire-and-forget way. It's deleting itself when
  74. * it has finished using deleteLater() so the job
  75. * instance disappears after the next event loop run.
  76. */
  77. class AbstractJob : public QObject
  78. {
  79. Q_OBJECT
  80. Q_ENUMS( KillVerbosity Capability Unit )
  81. Q_FLAGS( Capabilities )
  82.  
  83. public:
  84. enum Unit {
  85. Bytes,
  86. Files,
  87. Directories
  88. };
  89.  
  90. enum Capability {
  91. NoCapabilities = 0x0000,
  92. Killable = 0x0001,
  93. Suspendable = 0x0002
  94. };
  95.  
  96. Q_DECLARE_FLAGS( Capabilities, Capability )
  97.  
  98. /**
  99. * Creates a new AbstractJob object.
  100. *
  101. * @param parent the parent QObject
  102. */
  103. explicit AbstractJob( QObject *parent = 0 );
  104.  
  105. /**
  106. * Destroys a AbstractJob object.
  107. */
  108. virtual ~AbstractJob();
  109.  
  110. /**
  111. * Returns the capabilities of this job.
  112. *
  113. * @return the capabilities that this job supports
  114. * @see setCapabilities()
  115. */
  116. Capabilities capabilities() const;
  117.  
  118. /**
  119. * Returns if the job was suspended with the suspend() call.
  120. *
  121. * @return if the job was suspended
  122. * @see suspend() resume()
  123. */
  124. bool isSuspended() const;
  125.  
  126. /**
  127. * Starts the job asynchronously. When the job is finished,
  128. * result() is emitted.
  129. *
  130. * Warning: Never implement any synchronous workload in this method. This method
  131. * should just trigger the job startup, not do any work itself. It is expected to
  132. * be non-blocking.
  133. *
  134. * This is the method all subclasses need to implement.
  135. * It should setup and trigger the workload of the job. It should not do any
  136. * work itself. This includes all signals and terminating the job, e.g. by
  137. * emitResult(). The workload, which could be another method of the
  138. * subclass, is to be triggered using the event loop, e.g. by code like:
  139. * \code
  140. * void ExampleJob::start()
  141. * {
  142. * QTimer::singleShot( 0, this, SLOT( doWork() ) );
  143. * }
  144. * \endcode
  145. */
  146. virtual void start() = 0;
  147.  
  148. enum KillVerbosity { Quietly, EmitResult };
  149.  
  150. public Q_SLOTS:
  151. /**
  152. * Aborts this job.
  153. * This kills and deletes the job.
  154. *
  155. * @param verbosity if equals to EmitResult, Job will emit signal result
  156. * and ask uiserver to close the progress window.
  157. * @p verbosity is set to EmitResult for subjobs. Whether applications
  158. * should call with Quietly or EmitResult depends on whether they rely
  159. * on result being emitted or not. Please notice that if @p verbosity is
  160. * set to Quietly, signal result will NOT be emitted.
  161. * @return true if the operation is supported and succeeded, false otherwise
  162. */
  163. bool kill( KillVerbosity verbosity = Quietly );
  164.  
  165. /**
  166. * Suspends this job.
  167. * The job should be kept in a state in which it is possible to resume it.
  168. *
  169. * @return true if the operation is supported and succeeded, false otherwise
  170. */
  171. bool suspend();
  172.  
  173. /**
  174. * Resumes this job.
  175. *
  176. * @return true if the operation is supported and succeeded, false otherwise
  177. */
  178. bool resume();
  179.  
  180. protected:
  181. /**
  182. * Aborts this job quietly.
  183. * This simply kills the job, no error reporting or job deletion should be involved.
  184. *
  185. * @return true if the operation is supported and succeeded, false otherwise
  186. */
  187. virtual bool doKill();
  188.  
  189. /**
  190. * Suspends this job.
  191. *
  192. * @return true if the operation is supported and succeeded, false otherwise
  193. */
  194. virtual bool doSuspend();
  195.  
  196. /**
  197. * Resumes this job.
  198. *
  199. * @return true if the operation is supported and succeeded, false otherwise
  200. */
  201. virtual bool doResume();
  202.  
  203. /**
  204. * Sets the capabilities for this job.
  205. *
  206. * @param capabilities are the capabilities supported by this job
  207. * @see capabilities()
  208. */
  209. void setCapabilities( Capabilities capabilities );
  210.  
  211. public:
  212. /**
  213. * Executes the job synchronously.
  214. *
  215. * This will start a nested QEventLoop internally. Nested event loop can be dangerous and
  216. * can have unintended side effects, you should avoid calling exec() whenever you can and use the
  217. * asynchronous interface of AbstractJob instead.
  218. *
  219. * Should you indeed call this method, you need to make sure that all callers are reentrant,
  220. * so that events delivered by the inner event loop do not cause non-reentrant functions to be
  221. * called, which usually wreaks havoc.
  222. *
  223. * Note that the event loop started by this method does not process user input events, which means
  224. * your user interface will effectivly be blocked. Other events like paint or network events are
  225. * still being processed. The advantage of not processing user input events is that the chance of
  226. * accidental reentrancy is greatly reduced. Still you should avoid calling this function.
  227. *
  228. * @return true if the job has been executed without error, false otherwise
  229. */
  230. bool exec();
  231.  
  232. enum
  233. {
  234. NoError = 0,
  235. KilledJobError = 1,
  236. UserDefinedError = 100
  237. };
  238.  
  239.  
  240. /**
  241. * Returns the error code, if there has been an error.
  242. * Only call this method from the slot connected to result().
  243. *
  244. * @return the error code for this job, 0 if no error.
  245. */
  246. int error() const;
  247.  
  248. /**
  249. * Returns the error text if there has been an error.
  250. * Only call if error is not 0.
  251. * This is really internal, better use errorString.
  252. *
  253. * @return a string to help understand the error, usually the url
  254. * related to the error. Only valid if error() is not 0.
  255. */
  256. QString errorText() const;
  257.  
  258. /**
  259. * Converts an error code and a non-i18n error message into an
  260. * error message in the current language. The low level (non-i18n)
  261. * error message (usually a url) is put into the translated error
  262. * message using %1.
  263. *
  264. * Example for errid == ERR_CANNOT_OPEN_FOR_READING:
  265. * \code
  266. * i18n( "Could not read\n%1" , errorText() );
  267. * \endcode
  268. * Only call if error is not 0.
  269. *
  270. * @return the error message and if there is no error, a message
  271. * telling the user that the app is broken, so check with
  272. * error() whether there is an error
  273. */
  274. virtual QString errorString() const;
  275.  
  276.  
  277. /**
  278. * Returns the processed amount of a given unit for this job.
  279. *
  280. * @param unit the unit of the requested amount
  281. * @return the processed size
  282. */
  283. qulonglong processedAmount(Unit unit) const;
  284.  
  285. /**
  286. * Returns the total amount of a given unit for this job.
  287. *
  288. * @param unit the unit of the requested amount
  289. * @return the total size
  290. */
  291. qulonglong totalAmount(Unit unit) const;
  292.  
  293. /**
  294. * Returns the overall progress of this job.
  295. *
  296. * @return the overall progress of this job
  297. */
  298. unsigned long percent() const;
  299.  
  300. /**
  301. * Set the auto-delete property of the job. If @p autodelete is
  302. * set to false the job will not delete itself once it is finished.
  303. *
  304. * The default for any AbstractJob is to automatically delete itself.
  305. *
  306. * @param autodelete set to false to disable automatic deletion
  307. * of the job.
  308. */
  309. void setAutoDelete( bool autodelete );
  310.  
  311. /**
  312. * Returns whether this job automatically deletes itself once
  313. * the job is finished.
  314. *
  315. * @return whether the job is deleted automatically after
  316. * finishing.
  317. */
  318. bool isAutoDelete() const;
  319.  
  320. Q_SIGNALS:
  321. #if !defined(Q_MOC_RUN) && !defined(DOXYGEN_SHOULD_SKIP_THIS) && !defined(IN_IDE_PARSER)
  322. private: // do not tell moc, doxygen or kdevelop, but those signals are in fact private
  323. #endif
  324. /**
  325. * Emitted when the job is finished, in any case. It is used to notify
  326. * observers that the job is terminated and that progress can be hidden.
  327. *
  328. * This is a private signal, it cannot be emitted directly by subclasses of
  329. * AbstractJob, use emitResult() instead.
  330. *
  331. * Client code is not supposed to connect to this signal, signal result should
  332. * be used instead.
  333. *
  334. * If you store a list of jobs and they might get killed silently,
  335. * then you must connect to this instead of result().
  336. *
  337. * @param job the job that emitted this signal
  338. * @internal
  339. *
  340. * @see result
  341. */
  342. void finished(AbstractJob *job);
  343.  
  344. /**
  345. * Emitted when the job is suspended.
  346. *
  347. * This is a private signal, it cannot be emitted directly by subclasses of
  348. * AbstractJob.
  349. *
  350. * @param job the job that emitted this signal
  351. */
  352. void suspended(AbstractJob *job);
  353.  
  354. /**
  355. * Emitted when the job is resumed.
  356. *
  357. * This is a private signal, it cannot be emitted directly by subclasses of
  358. * AbstractJob.
  359. *
  360. * @param job the job that emitted this signal
  361. */
  362. void resumed(AbstractJob *job);
  363.  
  364. /**
  365. * Emitted when the job is finished (except when killed with AbstractJob::Quietly).
  366. *
  367. * Use error to know if the job was finished with error.
  368. *
  369. * This is a private signal, it cannot be emitted directly by subclasses of
  370. * AbstractJob, use emitResult() instead.
  371. *
  372. * Please connect to this signal instead of finished.
  373. *
  374. * @param job the job that emitted this signal
  375. *
  376. * @see kill
  377. */
  378. void result(AbstractJob *job);
  379.  
  380. Q_SIGNALS:
  381. /**
  382. * Emitted to display general description of this job. A description has
  383. * a title and two optional fields which can be used to complete the
  384. * description.
  385. *
  386. * Examples of titles are "Copying", "Creating resource", etc.
  387. * The fields of the description can be "Source" with an URL, and,
  388. * "Destination" with an URL for a "Copying" description.
  389. * @param job the job that emitted this signal
  390. * @param title the general description of the job
  391. * @param field1 first field (localized name and value)
  392. * @param field2 second field (localized name and value)
  393. */
  394. void description(AbstractJob *job, const QString &title,
  395. const QPair<QString, QString> &field1 = qMakePair(QString(), QString()),
  396. const QPair<QString, QString> &field2 = qMakePair(QString(), QString()));
  397.  
  398. /**
  399. * Emitted to display state information about this job.
  400. * Examples of message are "Resolving host", "Connecting to host...", etc.
  401. *
  402. * @param job the job that emitted this signal
  403. * @param plain the info message
  404. * @param rich the rich text version of the message, or QString() is none is available
  405. */
  406. void infoMessage( AbstractJob *job, const QString &plain, const QString &rich = QString() );
  407.  
  408. /**
  409. * Emitted to display a warning about this job.
  410. *
  411. * @param job the job that emitted this signal
  412. * @param plain the warning message
  413. * @param rich the rich text version of the message, or QString() is none is available
  414. */
  415. void warning( AbstractJob *job, const QString &plain, const QString &rich = QString() );
  416.  
  417.  
  418. Q_SIGNALS:
  419. #if !defined(Q_MOC_RUN) && !defined(DOXYGEN_SHOULD_SKIP_THIS) && !defined(IN_IDE_PARSER)
  420. private: // do not tell moc, doxygen or kdevelop, but those signals are in fact private
  421. #endif
  422. /**
  423. * Emitted when we know the amount the job will have to process. The unit of this
  424. * amount is sent too. It can be emitted several times if the job manages several
  425. * different units.
  426. *
  427. * This is a private signal, it can't be emitted directly by subclasses of
  428. * AbstractJob, use setTotalAmount() instead.
  429. *
  430. * @param job the job that emitted this signal
  431. * @param unit the unit of the total amount
  432. * @param amount the total amount
  433. */
  434. void totalAmount(AbstractJob *job, AbstractJob::Unit unit, qulonglong amount);
  435.  
  436. /**
  437. * Regularly emitted to show the progress of this job by giving the current amount.
  438. * The unit of this amount is sent too. It can be emitted several times if the job
  439. * manages several different units.
  440. *
  441. * This is a private signal, it can't be emitted directly by subclasses of
  442. * AbstractJob, use setProcessedAmount() instead.
  443. *
  444. * @param job the job that emitted this signal
  445. * @param unit the unit of the processed amount
  446. * @param amount the processed amount
  447. */
  448. void processedAmount(AbstractJob *job, AbstractJob::Unit unit, qulonglong amount);
  449.  
  450. /**
  451. * Emitted when we know the size of this job (data size in bytes for transfers,
  452. * number of entries for listings, etc).
  453. *
  454. * This is a private signal, it can't be emitted directly by subclasses of
  455. * AbstractJob, use setTotalAmount() instead.
  456. *
  457. * @param job the job that emitted this signal
  458. * @param size the total size
  459. */
  460. void totalSize(AbstractJob *job, qulonglong size);
  461.  
  462. /**
  463. * Regularly emitted to show the progress of this job
  464. * (current data size in bytes for transfers, entries listed, etc.).
  465. *
  466. * This is a private signal, it can't be emitted directly by subclasses of
  467. * AbstractJob, use setProcessedAmount() instead.
  468. *
  469. * @param job the job that emitted this signal
  470. * @param size the processed size
  471. */
  472. void processedSize(AbstractJob *job, qulonglong size);
  473.  
  474. /**
  475. * Progress signal showing the overall progress of the job
  476. * This is valid for any kind of job, and allows using a
  477. * a progress bar very easily. (see KProgressBar).
  478. * Note that this signal is not emitted for finished jobs.
  479. *
  480. * This is a private signal, it can't be emitted directly by subclasses of
  481. * AbstractJob, use emitPercent(), setPercent() setTotalAmount() or
  482. * setProcessedAmount() instead.
  483. *
  484. * @param job the job that emitted this signal
  485. * @param percent the percentage
  486. */
  487. void percent( AbstractJob *job, unsigned long percent );
  488.  
  489. /**
  490. * Emitted to display information about the speed of this job.
  491. *
  492. * This is a private signal, it can't be emitted directly by subclasses of
  493. * AbstractJob, use emitSpeed() instead.
  494. *
  495. * @param job the job that emitted this signal
  496. * @param speed the speed in bytes/s
  497. */
  498. void speed(AbstractJob *job, unsigned long speed);
  499.  
  500. protected:
  501. /**
  502. * Sets the error code. It should be called when an error
  503. * is encountered in the job, just before calling emitResult().
  504. *
  505. * @param errorCode the error code
  506. * @see emitResult()
  507. */
  508. void setError( int errorCode );
  509.  
  510. /**
  511. * Sets the error text. It should be called when an error
  512. * is encountered in the job, just before calling emitResult().
  513. *
  514. * @param errorText the error text
  515. * @see emitResult()
  516. */
  517. void setErrorText( const QString &errorText );
  518.  
  519.  
  520. /**
  521. * Sets the processed size. The processedAmount() and percent() signals
  522. * are emitted if the values changed. The percent() signal is emitted
  523. * only for the progress unit.
  524. *
  525. * @param unit the unit of the new processed amount
  526. * @param amount the new processed amount
  527. */
  528. void setProcessedAmount(Unit unit, qulonglong amount);
  529.  
  530. /**
  531. * Sets the total size. The totalSize() and percent() signals
  532. * are emitted if the values changed. The percent() signal is emitted
  533. * only for the progress unit.
  534. *
  535. * @param unit the unit of the new total amount
  536. * @param amount the new total amount
  537. */
  538. void setTotalAmount(Unit unit, qulonglong amount);
  539.  
  540. /**
  541. * Sets the overall progress of the job. The percent() signal
  542. * is emitted if the value changed.
  543. *
  544. * @param percentage the new overall progress
  545. */
  546. void setPercent( unsigned long percentage );
  547.  
  548.  
  549. /**
  550. * Utility function to emit the result signal, and suicide this job.
  551. * It first notifies the observers to hide the progress for this job using
  552. * the finished() signal.
  553. *
  554. * @note: Deletes this job using deleteLater().
  555. *
  556. * @see result()
  557. * @see finished()
  558. */
  559. void emitResult();
  560.  
  561. /**
  562. * Utility function for inherited jobs.
  563. * Emits the percent signal if bigger than previous value,
  564. * after calculating it from the parameters.
  565. *
  566. * @param processedAmount the processed amount
  567. * @param totalAmount the total amount
  568. * @see percent()
  569. */
  570. void emitPercent( qulonglong processedAmount, qulonglong totalAmount );
  571.  
  572. /**
  573. * Utility function for inherited jobs.
  574. * Emits the speed signal and starts the timer for removing that info
  575. *
  576. * @param speed the speed in bytes/s
  577. */
  578. void emitSpeed(unsigned long speed);
  579.  
  580. protected:
  581. AbstractJobPrivate *const d;
  582. };
  583.  
  584. Q_DECLARE_OPERATORS_FOR_FLAGS( AbstractJob::Capabilities )
  585.  
  586. #endif // GLUONPLAYER_ABSTRACTJOB_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement