Skip to content

Commit 8ab5dbf

Browse files
committed
Add KDialog Backend
1 parent 08c1ea5 commit 8ab5dbf

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ into an existing project and compiled along with it
1010

1111
### Build Options
1212
```c
13-
#define SFD_BACKEND_WIN32 // Only For Windows (Make Sure To Link With 'comdlg32')
14-
#define SFD_BACKEND_ZENITY // Zenity Backend (Requires Zenity)
13+
#define SFD_BACKEND_WIN32 // Only For Windows (Make Sure To Link With 'comdlg32')
14+
#define SFD_BACKEND_ZENITY // Zenity Backend (Requires zenity)
15+
#define SFD_BACKEND_KDIALOG // KDialog Backend (Requires kdialog)
1516
```
1617
1718
---

src/sfd.c

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const char* sfd_open_dialog(sfd_Options* opt) {
116116

117117
#endif // SFD_BACKEND_WIN32
118118

119-
#ifdef SFD_BACKEND_ZENITY
119+
#ifndef SFD_BACKEND_WIN32
120120

121121
// For popen()/pclose()
122122
#define _POSIX_C_SOURCE 2
@@ -127,6 +127,100 @@ const char* sfd_open_dialog(sfd_Options* opt) {
127127
#include <stdlib.h>
128128
#include <string.h>
129129

130+
#endif // !SFD_BACKEND_WIN32
131+
132+
#ifdef SFD_BACKEND_KDIALOG
133+
134+
const char* sfd_open_dialog(sfd_Options* opt) {
135+
static char result_buf[2048];
136+
char buf[2048];
137+
char* p;
138+
const char *title;
139+
FILE *fp;
140+
int n, len;
141+
142+
last_error = NULL;
143+
144+
// fp = popen("kdialog --version", "r");
145+
// if (fp == NULL || pclose(fp) != 0) {
146+
// last_error = "could not open kdialog";
147+
// return NULL;
148+
// }
149+
150+
n = sprintf(buf, "kdialog");
151+
152+
if (opt->save) {
153+
n += sprintf(buf + n, " --getsavefilename");
154+
} else {
155+
n += sprintf(buf + n, " --getopenfilename");
156+
}
157+
158+
if (opt->title) {
159+
title = opt->title;
160+
} else {
161+
title = opt->save ? "Save File" : "Open File";
162+
}
163+
164+
n += sprintf(buf + n, " --title \"%s\"", title);
165+
166+
if (opt->path && opt->path[0] != '\0') {
167+
n += sprintf(buf + n, " \"");
168+
p = realpath(opt->path, buf + n);
169+
if (p == NULL) {
170+
last_error = "call to realpath() failed";
171+
return NULL;
172+
}
173+
n += strlen(buf + n);
174+
n += sprintf(buf + n, "/\"");
175+
} else {
176+
n += sprintf(buf + n, " \".\"");
177+
}
178+
179+
if (opt->filter) {
180+
char b[64];
181+
const char *p;
182+
n += sprintf(buf + n, " \"");
183+
184+
if (opt->filter_name) {
185+
n += sprintf(buf + n, "%s (", opt->filter_name);
186+
}
187+
188+
p = opt->filter;
189+
while (next_filter(b, &p)) {
190+
n += sprintf(buf + n, "%s ", b);
191+
}
192+
// Remove The Last Extra Space
193+
buf[n - 1] = '\0'; n--;
194+
195+
if (opt->filter_name) {
196+
n += sprintf(buf + n, ")");
197+
}
198+
199+
n += sprintf(buf + n, " | All Files (*)\"");
200+
} else {
201+
n += sprintf(buf + n, " \"All Files (*)\"");
202+
}
203+
204+
205+
fp = popen(buf, "r");
206+
len = fread(result_buf, 1, sizeof(result_buf) - 1, fp);
207+
pclose(fp);
208+
209+
if (len > 0) {
210+
result_buf[len - 1] = '\0';
211+
if (opt->save && opt->extension && !strstr(result_buf, opt->extension)) {
212+
sprintf(&result_buf[len - 1], ".%s", opt->extension);
213+
}
214+
return result_buf;
215+
}
216+
217+
return NULL;
218+
}
219+
220+
#endif // SFD_BACKEND_KDIALOG
221+
222+
#ifdef SFD_BACKEND_ZENITY
223+
130224
const char* sfd_open_dialog(sfd_Options* opt) {
131225
static char result_buf[2048];
132226
char buf[2048];

0 commit comments

Comments
 (0)