Tizen 2.0 Release
[apps/core/preloaded/myfiles.git] / src / common / file-operation / mf-request.c
1 /*
2  * Copyright 2013         Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20 #include <glib.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include "mf-request.h"
25
26 int flagMsg = 1;
27 pthread_mutex_t gLockMsg;
28 pthread_cond_t gCondMsg;
29
30 struct _mf_fo_req {
31         GCond *cond;
32         GMutex *lock;
33         const char *path;
34         char *new_name;
35         int flagCond;
36         mf_request_type request;
37 };
38
39 mf_fo_request *mf_request_new(void)
40 {
41         mf_fo_request *result = NULL;
42
43         result = malloc(sizeof(mf_fo_request));
44         if (result) {
45                 result->cond = NULL;
46                 result->path = NULL;
47                 result->new_name = NULL;
48                 result->flagCond = 1;
49                 result->request = MF_REQ_MERGE;
50         }
51         return result;
52 }
53
54 void mf_request_free(mf_fo_request *req)
55 {
56         if (req) {
57                 if (req->new_name) {
58                         free(req->new_name);
59                 }
60                 free(req);
61         }
62         return;
63 }
64
65 void mf_request_set_result(mf_fo_request *req, mf_request_type result)
66 {
67         if (req) {
68                 req->request = result;
69
70                 if (req->cond) {
71                         if (req->flagCond == 1) {
72                                 req->flagCond = 0;
73                                 g_cond_broadcast(req->cond);
74                         }
75                 }
76         }
77
78         return;
79 }
80
81 void mf_request_set_result_rename(mf_fo_request *req, const char *new_name)
82 {
83         if (req) {
84                 req->request = MF_REQ_RENAME;
85                 if (new_name) {
86                         req->new_name = strdup(new_name);
87                 }
88
89                 if (req->cond) {
90                         g_cond_broadcast(req->cond);
91                 }
92         }
93
94         return;
95 }
96
97 void mf_request_set_cond(mf_fo_request *req, GCond * cond)
98 {
99         if (req) {
100                 req->cond = cond;
101         }
102         return;
103 }
104
105 void mf_request_set_path(mf_fo_request *req, const char *path)
106 {
107         if (req) {
108                 req->path = path;
109         }
110         return;
111 }
112
113 char *mf_request_get_new_name(mf_fo_request *req)
114 {
115         char *new_name = NULL;
116         if (req) {
117                 new_name = req->new_name;
118                 req->new_name = NULL;
119         }
120         return new_name;
121 }
122
123 const char *mf_request_get_path(mf_fo_request *req)
124 {
125         if (req) {
126                 return req->path;
127         }
128         return NULL;
129 }
130
131 mf_request_type mf_request_get_result(mf_fo_request *req)
132 {
133         mf_request_type request = MF_REQ_NONE;
134         if (req) {
135                 request = req->request;
136         }
137         return request;
138 }
139
140
141 void mf_msg_request_handled_send()
142 {
143         pthread_mutex_lock(&gLockMsg);
144         if (flagMsg == 0) {
145                 flagMsg = 1;
146                 pthread_cond_signal(&gCondMsg);
147         }
148         pthread_mutex_unlock(&gLockMsg);
149 }
150
151 int mf_request_flag_get(mf_fo_request *req)
152 {
153         if (req) {
154                 return req->flagCond;
155         }
156         return -1;
157 }
158
159 void mf_request_flag_set(mf_fo_request *req, int value)
160 {
161         if (req) {
162                 req->flagCond = value;
163         }
164 }