summaryrefslogtreecommitdiff
path: root/src/doc-util.c
blob: 26b3f8651086a0400ef043333bc1af5b8696ef3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <storage.h>
#include "doc-list.h"
#include "doc-data.h"
#include "doc-util.h"
#include "doc-ui-util.h"

static int __externalStorageId = 0;

static bool _doc_util_getSupportedStorages_cb(int storageId,
											  storage_type_e type,
											  storage_state_e state,
											  const char *path,
											  void *userData)
{
	doc_dbg("");

	if (type == STORAGE_TYPE_EXTERNAL) {
		__externalStorageId = storageId;
		return false;
	}

	return true;
}

static void _doc_db_update_sdcard_info(int storage_id,
									   storage_state_e state,
									   void *user_data)
{
	doc_dbgW("Storage state changed!");
	DOC_CHECK(user_data);

	int error =
		storage_foreach_device_supported(_doc_util_getSupportedStorages_cb,
										 NULL);
	if (error == STORAGE_ERROR_NONE) {
		storage_state_e mmc_state;
		int ret = storage_get_state(__externalStorageId, &mmc_state);
		if (ret != STORAGE_ERROR_NONE) {
			doc_dbgE("storage_get_state failed!");
			return;
		}

		/* Update view */
		if (mmc_state == STORAGE_STATE_MOUNTED) {
			doc_dbg("mmc_state[%d]: STORAGE_STATE_MOUNTED", mmc_state);
			doc_update_view((doc_ugdata *) user_data,
							DOC_UPDATE_MMC_ADDED);
		} else if (mmc_state == STORAGE_STATE_REMOVED) {
			doc_dbg("mmc_state[%d]: STORAGE_STATE_REMOVED", mmc_state);
			doc_update_view((doc_ugdata *) user_data,
							DOC_UPDATE_MMC_REMOVED);
		} else if (mmc_state == STORAGE_STATE_UNMOUNTABLE) {
			doc_dbg("mmc_state[%d]: STORAGE_STATE_UNMOUNTABLE", mmc_state);
			doc_update_view((doc_ugdata *) user_data,
							DOC_UPDATE_MMC_REMOVED);
		}
	}
}

Eina_Bool doc_update_view(doc_ugdata * ugd, int mode)
{
	doc_dbg("mode: %d", mode);
	DOC_CHECK_FALSE(ugd);
	int ret = -1;

	ret = doc_update_genlist(ugd);

	if (ret != 0)
		return EINA_FALSE;

	return EINA_TRUE;
}

int doc_reg_db_update_noti(doc_ugdata * ugd)
{
	doc_dbg("");
	DOC_CHECK_VAL(ugd, -1);
	int error_code = 0;

	error_code =
		storage_set_state_changed_cb(__externalStorageId,
									 _doc_db_update_sdcard_info, ugd);
	if (error_code != STORAGE_ERROR_NONE)
		doc_dbgE("storage_set_state_changed_cb failed!");

	return 0;
}

int doc_dereg_db_update_noti(void)
{
	doc_dbg("");
	int error_code = -1;

	error_code =
		storage_unset_state_changed_cb(__externalStorageId,
									   _doc_db_update_sdcard_info);
	if (error_code != STORAGE_ERROR_NONE)
		doc_dbgE("storage_set_state_changed_cb failed!");

	return 0;
}

int doc_atoi(const char *number)
{
	char *endptr = NULL;
	long val = 0;

	errno = 0;

	val = strtol(number, &endptr, 10);

	if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
		|| (errno != 0 && val == 0)) {
		doc_dbgE("strtol, val = %ld", val);
		return -1;
	}

	if (endptr == number) {
		doc_dbgE("No digits were found, number = %s", number);
		return -1;
	}

	return (int) val;
}