HaikuDepot: notify work status from main window
[haiku.git] / src / apps / haikudepot / server / AbstractServerProcess.h
blobf8288b13e32cc33d9f825d67825884df5b807185
1 /*
2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #ifndef ABSTRACT_SERVER_PROCESS_H
7 #define ABSTRACT_SERVER_PROCESS_H
9 #include <HttpRequest.h>
10 #include <Json.h>
11 #include <String.h>
12 #include <Url.h>
14 #include "StandardMetaData.h"
15 #include "Stoppable.h"
18 #define APP_ERR_NOT_MODIFIED (B_APP_ERROR_BASE + 452)
19 #define APP_ERR_NO_DATA (B_APP_ERROR_BASE + 453)
22 typedef enum process_options {
23 SERVER_PROCESS_NO_NETWORKING = 1 << 0,
24 SERVER_PROCESS_PREFER_CACHE = 1 << 1,
25 SERVER_PROCESS_DROP_CACHE = 1 << 2
26 } process_options;
29 typedef enum process_state {
30 SERVER_PROCESS_INITIAL = 1,
31 SERVER_PROCESS_RUNNING = 2,
32 SERVER_PROCESS_COMPLETE = 3
33 } process_state;
36 /*! Clients are able to subclass from this 'interface' in order to accept
37 call-backs when a process has exited; either through success or through
38 failure.
41 class AbstractServerProcessListener {
42 public:
43 virtual void ServerProcessExited() = 0;
47 class AbstractServerProcess : public Stoppable {
48 public:
49 AbstractServerProcess(
50 AbstractServerProcessListener* listener,
51 uint32 options);
52 virtual ~AbstractServerProcess();
54 virtual const char* Name() = 0;
55 status_t Run();
56 status_t Stop();
57 status_t ErrorStatus();
58 bool IsRunning();
59 bool WasStopped();
61 protected:
62 virtual status_t RunInternal() = 0;
63 virtual status_t StopInternal();
65 virtual void GetStandardMetaDataPath(
66 BPath& path) const = 0;
67 virtual void GetStandardMetaDataJsonPath(
68 BString& jsonPath) const = 0;
70 status_t IfModifiedSinceHeaderValue(
71 BString& headerValue) const;
72 status_t IfModifiedSinceHeaderValue(
73 BString& headerValue,
74 const BPath& metaDataPath,
75 const BString& jsonPath) const;
77 status_t PopulateMetaData(
78 StandardMetaData& metaData,
79 const BPath& path,
80 const BString& jsonPath) const;
82 status_t ParseJsonFromFileWithListener(
83 BJsonEventListener *listener,
84 const BPath& path) const;
86 status_t DownloadToLocalFileAtomically(
87 const BPath& targetFilePath,
88 const BUrl& url);
90 status_t DeleteLocalFile(const BPath& currentFilePath);
92 status_t MoveDamagedFileAside(
93 const BPath& currentFilePath);
95 bool HasOption(uint32 flag);
96 bool ShouldAttemptNetworkDownload(
97 bool hasDataAlready);
99 static bool IsSuccess(status_t e);
101 private:
102 BLocker fLock;
103 AbstractServerProcessListener*
104 fListener;
105 bool fWasStopped;
106 process_state fProcessState;
107 status_t fErrorStatus;
108 uint32 fOptions;
110 BHttpRequest* fRequest;
112 process_state ProcessState();
113 void SetErrorStatus(status_t value);
114 void SetProcessState(process_state value);
116 status_t DownloadToLocalFile(
117 const BPath& targetFilePath,
118 const BUrl& url,
119 uint32 redirects, uint32 failures);
121 bool LooksLikeGzip(const char *pathStr) const;
125 #endif // ABSTRACT_SERVER_PROCESS_H