xref: /haiku/src/servers/registrar/mime/CreateAppMetaMimeThread.cpp (revision b2c7de82305294ddf7dd438eecf63f281ef33eba)
1 /*
2  * Copyright 2002-2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Tyler Dauwalder
7  *		Axel Dörfler, axeld@pinc-software.de
8  */
9 
10 
11 #include "CreateAppMetaMimeThread.h"
12 
13 #include <stdio.h>
14 
15 #include <AppFileInfo.h>
16 #include <Bitmap.h>
17 #include <fs_attr.h>
18 #include <MimeType.h>
19 #include <Node.h>
20 #include <Path.h>
21 #include <String.h>
22 
23 #include <mime/database_support.h>
24 
25 namespace BPrivate {
26 namespace Storage {
27 namespace Mime {
28 
29 
30 CreateAppMetaMimeThread::CreateAppMetaMimeThread(const char *name,
31 	int32 priority, BMessenger managerMessenger, const entry_ref *root,
32 	bool recursive, int32 force, BMessage *replyee)
33 	: MimeUpdateThread(name, priority, managerMessenger, root, recursive, force,
34 		replyee)
35 {
36 }
37 
38 
39 status_t
40 CreateAppMetaMimeThread::DoMimeUpdate(const entry_ref* ref, bool* _entryIsDir)
41 {
42 	if (ref == NULL)
43 		return B_BAD_VALUE;
44 
45 	BNode typeNode;
46 
47 	BFile file;
48 	status_t status = file.SetTo(ref, B_READ_ONLY);
49 	if (status < B_OK)
50 		return status;
51 
52 	bool isDir = file.IsDirectory();
53 	if (_entryIsDir != NULL)
54 		*_entryIsDir = isDir;
55 
56 	if (isDir)
57 		return B_OK;
58 
59 	BAppFileInfo appInfo(&file);
60 	status = appInfo.InitCheck();
61 	if (status < B_OK)
62 		return status;
63 
64 	// Read the app sig (which consequently keeps us from updating
65 	// non-applications, since we get an error if the file has no
66 	// app sig)
67 	BString signature;
68 	status = file.ReadAttrString("BEOS:APP_SIG", &signature);
69 	if (status < B_OK)
70 		return B_BAD_TYPE;
71 
72 	// Init our various objects
73 
74 	BMimeType mime;
75 	status = mime.SetTo(signature.String());
76 	if (status < B_OK)
77 		return status;
78 
79 	if (!mime.IsInstalled())
80 		mime.Install();
81 
82 	BString path = "/";
83 	path.Append(signature);
84 	path.ToLower();
85 		// Signatures and MIME types are case insensitive, but we want to
86 		// preserve the case wherever possible
87 	path.Prepend(kDatabaseDir.c_str());
88 
89 	status = typeNode.SetTo(path.String());
90 	if (status < B_OK)
91 		return status;
92 
93 	// Preferred App
94 	attr_info info;
95 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kPreferredAppAttr, &info) != B_OK))
96 		status = mime.SetPreferredApp(signature.String());
97 
98 	// Short Description (name of the application)
99 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kShortDescriptionAttr, &info) != B_OK))
100 		status = mime.SetShortDescription(ref->name);
101 
102 	// App Hint
103 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kAppHintAttr, &info) != B_OK))
104 		status = mime.SetAppHint(ref);
105 
106 	// Vector Icon
107 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kIconAttr, &info) != B_OK)) {
108 		uint8* data = NULL;
109 		size_t size = 0;
110 		if (appInfo.GetIcon(&data, &size) == B_OK) {
111 			status = mime.SetIcon(data, size);
112 			free(data);
113 		}
114 	}
115 	// Mini Icon
116 	BBitmap miniIcon(BRect(0, 0, 15, 15), B_BITMAP_NO_SERVER_LINK, B_CMAP8);
117 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kMiniIconAttr, &info) != B_OK)) {
118 		if (appInfo.GetIcon(&miniIcon, B_MINI_ICON) == B_OK)
119 			status = mime.SetIcon(&miniIcon, B_MINI_ICON);
120 	}
121 	// Large Icon
122 	BBitmap largeIcon(BRect(0, 0, 31, 31), B_BITMAP_NO_SERVER_LINK, B_CMAP8);
123 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kLargeIconAttr, &info) != B_OK)) {
124 		if (appInfo.GetIcon(&largeIcon, B_LARGE_ICON) == B_OK)
125 			status = mime.SetIcon(&largeIcon, B_LARGE_ICON);
126 	}
127 
128 	// Supported Types
129 	BMessage supportedTypes;
130 	if (status == B_OK && (fForce || typeNode.GetAttrInfo(kSupportedTypesAttr, &info) != B_OK)) {
131 		if (appInfo.GetSupportedTypes(&supportedTypes) == B_OK)
132 			status = mime.SetSupportedTypes(&supportedTypes);
133 	}
134 
135 	// Icons for supported types
136 	const char* type;
137 	for (int32 i = 0; supportedTypes.FindString("types", i, &type) == B_OK; i++) {
138 		// vector icon
139 		uint8* data = NULL;
140 		size_t size = 0;
141 		if (status == B_OK && appInfo.GetIconForType(type, &data, &size) == B_OK) {
142 			status = mime.SetIconForType(type, data, size);
143 			free(data);
144 		}
145 		// mini icon
146 		if (status == B_OK && appInfo.GetIconForType(type, &miniIcon, B_MINI_ICON) == B_OK)
147 			status = mime.SetIconForType(type, &miniIcon, B_MINI_ICON);
148 		// large icon
149 		if (status == B_OK && appInfo.GetIconForType(type, &largeIcon, B_LARGE_ICON) == B_OK)
150 			status = mime.SetIconForType(type, &largeIcon, B_LARGE_ICON);
151 	}
152 
153 	return status;
154 }
155 
156 }	// namespace Mime
157 }	// namespace Storage
158 }	// namespace BPrivate
159 
160