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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/********************************************************************************
* *
* F i l e A s s o c i a t i o n s *
* *
*********************************************************************************
* Copyright (C) 1998,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXFile.h"
#include "FXFileStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXDictionary.h"
#include "FXDictionaryOf.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXIcon.h"
#include "FXIconCache.h"
#include "FXFileAssociations.h"
/*
Notes:
- FXFileAssociations needs additional fields, e.g. a print command.
- The associate member function should be virtual so we can overload it.
- FXFileAssociations is solely responsible for determining mime-type, and
duplicate code in FXDirList and FXFileList is eliminated.
- We will use two different techniques:
- For directories, we will match "/usr/people/jeroen", then
try "/people/jeroen", and finally, try "/jeroen" to determine
directory bindings (Note we pass a "/" in front so we won't
match a file binding when looking for directories.
This means we can in many cases keep the same icon bindings
even if some directory tree of a project is moved around in
the file system.
- For files, we will try to match the whole name, then try
the extensions.
- We will try to match "defaultdirbinding" to determine directory type,
and "defaultfilebinding" for a file, and "defaultexecbinding", for an
executable to allow bindings to be set for broad categories.
- We should look into using the mime-database and content-based
file type detection [I'm not a big fan of this, but it may sometimes
be necessary].
- Refer to RFC 2045, 2046, 2047, 2048, and 2077.
The Internet media type registry is at:
ftp://ftp.iana.org/in-notes/iana/assignments/media-types/
- We should at least organize things so that enough info is passed in
so we can read a fragment of the content and do it (so that means
the full pathname, and perhaps some flags).
- The registry format has been extended; it now is:
command string ';' extension string ';' bigicon [ ':' bigiconopen ] ';' miniicon [ ':' miniiconopen ] ';' mimetype [ ';' flags ... ]
- An empty binding like:
ext=""
Can be used to override a global binding with an empty one, i.e. it is
as if no binding exists; you will get the default or fallback in this case.
- Obtaining icons for files, folders, executable, etc. under Windows:
1) Get icon sizes:
w = GetSystemMetrics(SM_CXSMICON);
h = GetSystemMetrics(SM_CYSMICON);
2) Default icons for folder:
// Get key
RegOpenKeyEx(HKEY_CLASSES_ROOT,"folder\\DefaultIcon",0,KEY_READ,&key);
// To get data
RegQueryValueEx(key,subKey,0,0,(LPBYTE)buf,&bufsz);
// Obtain HICON extract it
result=ExtractIconEx(filename,iconindex,arrayofbigicons,arrayofsmallicons,nicons);
3) Default icon(s) for file:
// Extract from shell32.dll
ExtractIconEx("shell32.dll",0,NULL,&smallicon,1);
4) Default exe icon(s):
// Extract from shell32.dll
ExtractIconExA("shell32.dll",2,NULL,&smallicon,1);
5) Other executables:
// Extract from executable:
ExtractIconEx("absolutepathofexe",indexoflast,NULL,&smallicon,1);
6) Documents:
// Key on ".bmp" (extension with . in front)
RegOpenKeyEx(HKEY_CLASSES_ROOT,".bmp",0,KEY_READ,&key);
// Obtain value using:
RegQueryValueEx(key,subKey,0,0,(LPBYTE)buf,&bufsz);
// Key on file type and look for default icon:
RegOpenKeyEx(HKEY_CLASSES_ROOT,"PaintPicture\\DefaultIcon",0,KEY_READ,&keyoficon);
// Obtain value using:
RegQueryValueEx(key,subKey,0,0,(LPBYTE)buf,&bufsz);
// String contains program pathname, and icon index. Use:
ExtractIconEx("absolutepathofprogram",index,NULL,&smallicon,1);
*/
#define TOPIC_CONSTRUCT 1000
#define COMMANDLEN 256
#define EXTENSIONLEN 128
#define MIMETYPELEN 64
#define ICONNAMELEN 256
using namespace FX;
/*******************************************************************************/
namespace FX {
// Object implementation
FXIMPLEMENT(FXFileAssociations,FXObject,nullptr,0)
// These registry keys are used for default bindings.
const FXchar FXFileAssociations::defaultExecBinding[]="defaultexecbinding";
const FXchar FXFileAssociations::defaultDirBinding[]="defaultdirbinding";
const FXchar FXFileAssociations::defaultFileBinding[]="defaultfilebinding";
// Construct for serialization
FXFileAssociations::FXFileAssociations():cache(nullptr),settings(nullptr){
}
// Construct an file-extension association table
FXFileAssociations::FXFileAssociations(FXApp* app):cache(app),settings(&app->reg()){
FXTRACE((TOPIC_CONSTRUCT,"FXFileAssociations::FXFileAssociations\n"));
cache.setIconPath(settings->readStringEntry("SETTINGS","iconpath",FXIconCache::defaultIconPath));
}
// Construct an file-extension association table, and alternative settings database
FXFileAssociations::FXFileAssociations(FXApp* app,FXSettings* sdb):cache(app),settings(sdb){
FXTRACE((TOPIC_CONSTRUCT,"FXFileAssociations::FXFileAssociations\n"));
cache.setIconPath(settings->readStringEntry("SETTINGS","iconpath",FXIconCache::defaultIconPath));
}
// Parse binding and populate association from it
FXFileAssoc* FXFileAssociations::parse(const FXString& assoc){
FXTRACE((200,"FXFileAssociations::parse(\"%s\")\n",assoc.text()));
if(!assoc.empty()){
FXFileAssoc* result=new FXFileAssoc;
if(result){
FXString mininameopen;
FXString bignameopen;
FXString mininame;
FXString bigname;
FXString string;
FXuint flags=0;
// Parse command
result->command=assoc.section(';',0);
// Parse description
result->extension=assoc.section(';',1);
// Big icon closed and open
string=assoc.section(';',2);
bigname=string.section(':',0);
bignameopen=string.section(':',1);
// Small icon closed and open
string=assoc.section(';',3);
mininame=string.section(':',0);
mininameopen=string.section(':',1);
// Initialize icons
result->bigicon=nullptr;
result->miniicon=nullptr;
result->bigiconopen=nullptr;
result->miniiconopen=nullptr;
// Insert icons into icon dictionary
if(!bigname.empty()){ result->bigicon=result->bigiconopen=cache.insert(bigname); }
if(!mininame.empty()){ result->miniicon=result->miniiconopen=cache.insert(mininame); }
// Add open icons also; we will fall back on the regular icons in needed
if(!bignameopen.empty()){ result->bigiconopen=cache.insert(bignameopen); }
if(!mininameopen.empty()){ result->miniiconopen=cache.insert(mininameopen); }
// Parse mime type
result->mimetype=assoc.section(';',4);
// Drag type will be set later
result->dragtype=0;
// Parse flags
string=assoc.section(';',5);
if(string.contains("c")) flags|=1;
if(string.contains("t")) flags|=2;
result->flags=flags;
FXTRACE((300,"FXFileAssociations::parse: command=\"%s\" extension=\"%s\" mimetype=\"%s\" big=\"%s\" bigopen=\"%s\" mini=\"%s\" miniopen=\"%s\" flags=%d\n",result->command.text(),result->extension.text(),result->mimetype.text(),bigname.text(),bignameopen.text(),mininame.text(),mininameopen.text(),flags));
// Return it
return result;
}
}
return nullptr;
}
// Fetch file association
FXFileAssoc* FXFileAssociations::fetch(const FXString& ext){
FXTRACE((200,"FXFileAssociations::fetch(\"%s\")\n",ext.text()));
FXFileAssoc* result=bindings[ext];
if(!result){
result=parse(getSettings()->readStringEntry("FILETYPES",ext,nullptr));
if(result){
bindings[ext]=result;
}
}
return result;
}
// Find file association from registry
FXFileAssoc* FXFileAssociations::findFileBinding(const FXString& pathname){
FXTRACE((200,"FXFileAssociations::findFileBinding(\"%s\")\n",pathname.text()));
const FXchar* filename=pathname.text();
FXFileAssoc* record;
for(const FXchar* p=filename; *p; ++p){
if(ISPATHSEP(*p)) filename=p+1;
}
while(*filename!='\0'){
if((record=fetch(filename))!=nullptr) return record;
filename=strchr(filename,'.');
if(!filename) break;
filename++;
}
return fetch(defaultFileBinding);
}
// Find directory association from registry
FXFileAssoc* FXFileAssociations::findDirBinding(const FXString& pathname){
FXTRACE((200,"FXFileAssociations::findDirBinding(\"%s\")\n",pathname.text()));
const FXchar* path=pathname.text();
FXFileAssoc* record;
while(*path){
if((record=fetch(path))!=nullptr) return record;
while(ISPATHSEP(*path)) path++; // Skip path seperators
while(*path && !ISPATHSEP(*path)) path++; // Skip directory name
}
return fetch(defaultDirBinding);
}
// Find executable association from registry
FXFileAssoc* FXFileAssociations::findExecBinding(const FXString& pathname){
FXTRACE((200,"FXFileAssociations::findExecBinding(\"%s\")\n",pathname.text()));
return fetch(defaultExecBinding);
}
// Delete all icons
void FXFileAssociations::clear(){
for(FXival i=0; i<bindings.no(); ++i){
delete bindings.data(i);
}
bindings.clear();
cache.clear();
}
// Save object to stream
void FXFileAssociations::save(FXStream& store) const {
cache.save(store);
}
// Load object from stream
void FXFileAssociations::load(FXStream& store){
cache.load(store);
}
// Destructor
FXFileAssociations::~FXFileAssociations(){
FXTRACE((TOPIC_CONSTRUCT,"FXFileAssociations::~FXFileAssociations\n"));
clear();
settings=(FXSettings*)-1L;
}
}
|