4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://floralicense.org/license/
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sys/types.h>
31 #include "pt_common.h"
34 #define GETPPD "/usr/bin/getppd"
35 #define CUPS_PPDDIR "/opt/etc/cups/ppd/"
36 #define SAMSUNG_DRV "/opt/etc/cups/ppd/samsung/samsung.drv"
37 #define SAMSUNG_DRV_GZ "/opt/etc/cups/ppd/samsung/samsung.drv.gz"
38 #define SAMSUNG_DRV_GZ_ORG "/usr/share/cups/ppd/samsung/samsung.drv.gz"
39 #define HP_DRV "/opt/etc/cups/ppd/hp/hp.drv"
40 #define HP_DRV_GZ "/opt/etc/cups/ppd/hp/hp.drv.gz"
41 #define HP_DRV_GZ_ORG "/usr/share/cups/ppd/hp/hp.drv.gz"
42 #define EPSON_DRV "/opt/etc/cups/ppd/epson/epson.drv"
43 #define EPSON_DRV_GZ "/opt/etc/cups/ppd/epson/epson.drv.gz"
44 #define EPSON_DRV_GZ_ORG "/usr/share/cups/ppd/epson/epson.drv.gz"
45 #define SAMSUNG_PPD_DIR "/opt/etc/cups/ppd/samsung"
46 #define EPSON_PPD_DIR "/opt/etc/cups/ppd/epson"
47 #define HP_PPD_DIR "/opt/etc/cups/ppd/hp"
49 #define PPDC_PREFIX "ppdc: Writing ./"
51 /*supported manufacturer printer*/
52 const char *manufacturer[MANUFACTURER_NUM] = {"Samsung", "Hp", "Epson"};
54 ppd_size_t *pt_utils_paper_size_pts(const char *name)
56 return ppdPageSize(ppd, name);
60 * This function let the app get ppd file for the specified printer
61 * @return If success, return PT_ERR_NONE, else return PT_ERR_FAIL
62 * @param[in] ppd the pointer to the printer's ppd path
63 * @param[in] printer the printer entry
65 int pt_get_printer_ppd(pt_printer_mgr_t *printer)
67 PRINT_SERVICE_FUNC_ENTER;
68 PT_RETV_IF(printer == NULL, PT_ERR_INVALID_PARAM, "printer is NULL");
69 PT_RETV_IF(printer->mfg == NULL, PT_ERR_INVALID_PARAM, "printer mfg is NULL");
70 PT_RETV_IF(printer->mdl == NULL, PT_ERR_INVALID_PARAM, "printer mdl is NULL");
72 PT_DEBUG("product: %s", printer->name);
73 PT_DEBUG("printer->mdl %s", printer->mdl);
74 PT_DEBUG("printer->mfg %s", printer->mfg);
78 char gunzip_drv[PT_MAX_LENGTH] = {0,};
80 static pt_db *db_samsung = NULL;
81 static pt_db *db_epson = NULL;
82 static pt_db *db_hp = NULL;
85 if (strncasecmp(printer->mfg, "Samsung", 7) == 0) {
86 if (chdir(SAMSUNG_PPD_DIR)) {
87 PT_DEBUG("Failed to change directory");
90 if (access(SAMSUNG_DRV, F_OK) != 0) {
91 int ret = _pt_filecopy(SAMSUNG_DRV_GZ_ORG, SAMSUNG_DRV_GZ);
93 PT_DEBUG("Failed to copy file");
96 snprintf(gunzip_drv, PT_MAX_LENGTH, "gzip -d %s", SAMSUNG_DRV_GZ);
97 result = popen(gunzip_drv, "r");
99 PT_DEBUG("%s is failed to popen error",gunzip_drv);
105 db_samsung = pt_create_db(SAMSUNG_DRV);
106 PT_DEBUG("db_samsung: %p", db_samsung);
108 PT_DEBUG("Failed to create samsung database");
112 output = pt_extract_ppd(db_samsung, printer->mdl, PT_SEARCH_ALL);
114 } else if (strncasecmp(printer->mfg, "Hp", 2) == 0) {
115 if (chdir(HP_PPD_DIR)) {
116 PT_DEBUG("Failed to change directory");
119 if (access(HP_DRV, F_OK) != 0) {
120 int ret = _pt_filecopy(HP_DRV_GZ_ORG, HP_DRV_GZ);
122 PT_DEBUG("Failed to copy file");
125 snprintf(gunzip_drv, PT_MAX_LENGTH, "gzip -d %s", HP_DRV_GZ);
126 result = popen(gunzip_drv, "r");
127 if (result == NULL) {
128 PT_DEBUG("%s is failed to popen error",gunzip_drv);
134 db_hp = pt_create_db(HP_DRV);
136 PT_DEBUG("Failed to create hp database");
140 output = pt_extract_ppd(db_hp, printer->mdl, PT_SEARCH_ALL);
141 } else if (strncasecmp(printer->mfg, "Epson", 5) == 0) {
142 if (chdir(EPSON_PPD_DIR)) {
143 PT_DEBUG("Failed to change directory");
146 if (access(EPSON_DRV, F_OK) != 0) {
147 int ret = _pt_filecopy(EPSON_DRV_GZ_ORG, EPSON_DRV_GZ);
149 PT_DEBUG("Failed to copy file");
152 snprintf(gunzip_drv, PT_MAX_LENGTH, "gzip -d %s", EPSON_DRV_GZ);
153 result = popen(gunzip_drv, "r");
154 if (result == NULL) {
155 PT_DEBUG("%s is failed to popen error",gunzip_drv);
161 db_epson = pt_create_db(EPSON_DRV);
163 PT_DEBUG("Failed to create epson database");
167 output = pt_extract_ppd(db_epson, printer->mdl, PT_SEARCH_ALL);
169 PT_DEBUG("Can't find PPD file");
174 PT_DEBUG("Can't find PPD file");
178 char *filename = NULL;
179 gboolean res = g_str_has_prefix(output, PPDC_PREFIX);
181 size_t len = strlen(PPDC_PREFIX);
182 filename = output+len;
183 size_t sublen = strlen(filename);
184 gboolean expected_suffix = g_str_has_suffix(filename, ".\n");
185 if(expected_suffix == TRUE) {
186 filename[sublen-2] = '\0';
188 PT_DEBUG("ppdc returned unexpected output:\n%s\n", output);
193 PT_DEBUG("ppdc returned unexpected output:\n%s\n", output);
198 char *abspath = realpath(filename, NULL);
200 PT_DEBUG("pathname canonicalization fails\n");
205 if(strlen(abspath) >= PT_MAX_LENGTH) {
206 PT_DEBUG("ppd filename too long\n");
211 memset(printer->ppd, '\0', PT_MAX_LENGTH);
213 strncpy(printer->ppd, abspath, PT_MAX_LENGTH-1);
216 PRINT_SERVICE_FUNC_LEAVE;