f85f47654de7cf076c787dd3532997b24b911eea
[citadel.git] / libcitadel / tests / mime_xdg_lookup_type.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <dirent.h>
8 #include <errno.h>
9
10 #include <fcntl.h>
11
12 #include <unistd.h>
13 #include <stddef.h>
14
15
16 #include "../lib/libcitadel.h"
17
18 int main(int argc, char* argv[])
19 {
20         char a;
21         int fd;
22         char *filename = NULL;
23         char *IconDir = NULL;
24         struct stat statbuf;
25         const char *Err;
26
27         StrBuf *MimeBuf;
28         long MimeLen;
29         char *MimeStr;
30         int by_extension = 0;
31         const char *GuessedMimeType;
32         const char *GuessedMimeIcon;
33         char MimeTypeStr[SIZ];
34
35         setvbuf(stdout, NULL, _IONBF, 0);
36
37
38         while ((a = getopt(argc, argv, "xf:i:")) != EOF)
39         {
40                 switch (a) {
41                 case 'x':
42                         by_extension = 1;
43                         break;
44                 case 'f':
45                         filename = optarg;
46                         break;
47                 case 'i':
48                         IconDir = optarg;
49                         break;
50                 }
51         }
52         StartLibCitadel(8);
53
54         if (IconDir != NULL)
55                 LoadIconDir(IconDir);
56
57         if (filename == NULL) {
58                 ShutDownLibCitadel();
59                 printf("Filename requried! -f\n");
60                 return 1;
61         }
62
63         if (by_extension) {
64                 GuessedMimeType = GuessMimeByFilename(filename, strlen(filename));
65                 printf("Mimetype: %s\n", GuessedMimeType);
66                 if (IconDir != NULL) {
67                         strcpy(MimeTypeStr, GuessedMimeType);
68                         GuessedMimeIcon = GetIconFilename(MimeTypeStr, strlen(MimeTypeStr));
69                         if (GuessedMimeIcon != NULL)
70                                 printf("Associated Icon [%s]\n", GuessedMimeIcon);
71                         else 
72                                 printf("no icon associated.\n");
73                 }
74                 ShutDownLibCitadel();
75                 return 0;
76         }
77
78         fd = open(filename, 0);
79         if (fd < 0) {
80                 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
81                 ShutDownLibCitadel();
82                 return 1;
83         }
84         if (fstat(fd, &statbuf) == -1) {
85                 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
86                 ShutDownLibCitadel();
87                 return 1;
88         }
89         MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
90         if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
91                 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
92                 FreeStrBuf(&MimeBuf);
93                 ShutDownLibCitadel();
94                 return 1;
95         }
96         MimeLen = StrLength(MimeBuf);
97         MimeStr = SmashStrBuf(&MimeBuf);
98         GuessedMimeType = GuessMimeType(MimeStr, MimeLen);
99
100         printf("Mimetype: %s\n", GuessedMimeType);
101         if (IconDir != NULL) {
102                 strcpy(MimeTypeStr, GuessedMimeType);
103                 GuessedMimeIcon = GetIconFilename(MimeTypeStr, strlen(MimeTypeStr));
104                 if (GuessedMimeIcon != NULL)
105                         printf("Associated Icon [%s]\n", GuessedMimeIcon);
106                 else 
107                         printf("no icon associated.\n");
108         }
109
110
111         free(MimeStr);
112         ShutDownLibCitadel();
113         return 0;
114 }