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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
|
/*
* Copyright 2005-2012 Fabrice Colin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <strings.h>
#include <unistd.h>
#include <glib.h>
#include <iostream>
#include <cstring>
#ifndef USE_GIO
#include "xdgmime/xdgmime.h"
#endif
#include "MIMEScanner.h"
#include "StringManip.h"
#include "Url.h"
#define APPLICATIONS_DIRECTORY "/share/applications/"
#define MIME_DEFAULTS_LIST "defaults.list"
#define MIME_DEFAULTS_SECTION "Default Applications"
#define MIME_CACHE "mimeinfo.cache"
#define MIME_CACHE_SECTION "MIME Cache"
#define UNKNOWN_MIME_TYPE "application/octet-stream";
using std::clog;
using std::endl;
using std::string;
using std::list;
using std::map;
using std::multimap;
using std::set;
using std::vector;
using std::pair;
#ifndef USE_GIO
static string getKeyValue(GKeyFile *pDesktopFile, const string &key)
{
string value;
if ((pDesktopFile == NULL) ||
(key.empty() == true))
{
return "";
}
GError *pError = NULL;
gchar *pKeyValue = g_key_file_get_string(pDesktopFile,
"Desktop Entry", key.c_str(), &pError);
if (pError == NULL)
{
if (pKeyValue != NULL)
{
value = pKeyValue;
g_free(pKeyValue);
}
}
else
{
g_error_free(pError);
}
return value;
}
#endif
MIMEAction::MIMEAction() :
m_multipleArgs(false),
m_localOnly(true),
#ifdef USE_GIO
m_pAppInfo(NULL)
#else
m_icon(""),
m_device("")
#endif
{
}
MIMEAction::MIMEAction(const string &name, const string &cmdLine) :
m_multipleArgs(false),
m_localOnly(true),
m_name(name),
m_exec(cmdLine),
#ifdef USE_GIO
m_pAppInfo(NULL)
#else
m_icon(""),
m_device("")
#endif
{
#ifdef USE_GIO
GError *pError = NULL;
m_pAppInfo = g_app_info_create_from_commandline(cmdLine.c_str(),
name.c_str(), G_APP_INFO_CREATE_SUPPORTS_URIS, &pError);
if (pError != NULL)
{
g_error_free(pError);
}
#else
parseExec();
#endif
}
#ifdef USE_GIO
MIMEAction::MIMEAction(GAppInfo *pAppInfo) :
m_multipleArgs(false),
m_localOnly(true),
m_pAppInfo(NULL)
{
if (pAppInfo != NULL)
{
const char *pInfo = g_app_info_get_name(pAppInfo);
if (pInfo != NULL)
{
m_name = pInfo;
}
pInfo = g_app_info_get_executable(pAppInfo);
if (pInfo != NULL)
{
m_exec = pInfo;
}
if (g_app_info_supports_uris(pAppInfo) == TRUE)
{
m_localOnly = false;
}
m_pAppInfo = g_app_info_dup(pAppInfo);
}
}
#else
MIMEAction::MIMEAction(const string &desktopFile) :
m_multipleArgs(false),
m_localOnly(true),
m_location(desktopFile)
{
load();
}
#endif
MIMEAction::MIMEAction(const MIMEAction &other) :
m_multipleArgs(other.m_multipleArgs),
m_localOnly(other.m_localOnly),
m_name(other.m_name),
m_location(other.m_location),
m_exec(other.m_exec),
#ifdef USE_GIO
m_pAppInfo(NULL)
#else
m_icon(other.m_icon),
m_device(other.m_device)
#endif
{
#ifdef USE_GIO
if (other.m_pAppInfo != NULL)
{
m_pAppInfo = g_app_info_dup(other.m_pAppInfo);
}
#endif
}
MIMEAction::~MIMEAction()
{
#ifdef USE_GIO
if (m_pAppInfo != NULL)
{
g_object_unref(m_pAppInfo);
}
#endif
}
bool MIMEAction::operator<(const MIMEAction &other) const
{
if (m_name < other.m_name)
{
return true;
}
return false;
}
MIMEAction &MIMEAction::operator=(const MIMEAction &other)
{
if (this != &other)
{
m_multipleArgs = other.m_multipleArgs;
m_localOnly = other.m_localOnly;
m_name = other.m_name;
m_location = other.m_location;
m_exec = other.m_exec;
#ifdef USE_GIO
if (m_pAppInfo != NULL)
{
g_object_unref(m_pAppInfo);
m_pAppInfo = NULL;
}
if (other.m_pAppInfo != NULL)
{
m_pAppInfo = g_app_info_dup(other.m_pAppInfo);
}
#else
m_icon = other.m_icon;
m_device = other.m_device;
#endif
}
return *this;
}
#ifndef USE_GIO
void MIMEAction::load(void)
{
GKeyFile *pDesktopFile = g_key_file_new();
GError *pError = NULL;
if (pDesktopFile != NULL)
{
g_key_file_load_from_file(pDesktopFile, (const gchar *)m_location.c_str(),
G_KEY_FILE_NONE, &pError);
if (pError == NULL)
{
m_name = getKeyValue(pDesktopFile, "Name");
// This may contain parameters described here :
// http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
m_exec = getKeyValue(pDesktopFile, "Exec");
m_icon = getKeyValue(pDesktopFile, "Icon");
m_device = getKeyValue(pDesktopFile, "Device");
parseExec();
}
else
{
g_error_free(pError);
}
g_key_file_free(pDesktopFile);
}
}
void MIMEAction::parseExec(void)
{
// Does it support multiple arguments ?
if ((m_exec.find("%U") != string::npos) ||
(m_exec.find("%F") != string::npos) ||
(m_exec.find("%D") != string::npos) ||
(m_exec.find("%N") != string::npos))
{
// Yes, it does
m_multipleArgs = true;
}
// What about URLs as parameters ?
if ((m_exec.find("%u") != string::npos) ||
(m_exec.find("%U") != string::npos))
{
// It does, therefore it's not exclusively local
m_localOnly = false;
}
}
MIMECache::MIMECache()
{
}
MIMECache::MIMECache(const string &file, const string §ion) :
m_file(file),
m_section(section)
{
}
MIMECache::MIMECache(const MIMECache &other) :
m_file(other.m_file),
m_section(other.m_section),
m_defaultActions(other.m_defaultActions)
{
}
MIMECache::~MIMECache()
{
}
bool MIMECache::operator<(const MIMECache &other) const
{
if (m_file < other.m_file)
{
return true;
}
return false;
}
MIMECache& MIMECache::operator=(const MIMECache &other)
{
if (this != &other)
{
m_file = other.m_file;
m_section = other.m_section;
m_defaultActions = other.m_defaultActions;
}
return *this;
}
bool MIMECache::findDesktopFile(const string &desktopFile, const string &mimeType,
map<string, MIMEAction> &loadedActions)
{
// Has already been loaded ?
map<string, MIMEAction>::const_iterator actionIter = loadedActions.find(desktopFile);
if (actionIter != loadedActions.end())
{
// Yes, it was so just copy it
m_defaultActions.insert(pair<string, MIMEAction>(mimeType, actionIter->second));
return true;
}
// Does it exist in that path ?
else if (access(desktopFile.c_str(), F_OK) == 0)
{
// It's here, we need to read it off the disk
MIMEAction typeAction(desktopFile);
#ifdef DEBUG
clog << "MIMECache::findDesktopFile: read " << desktopFile << endl;
#endif
if (typeAction.m_exec.empty() == false)
{
m_defaultActions.insert(pair<string, MIMEAction>(mimeType, typeAction));
loadedActions.insert(pair<string, MIMEAction>(desktopFile, typeAction));
return true;
}
}
return false;
}
void MIMECache::reload(const list<string> &desktopFilesPaths)
{
if (m_file.empty() == true)
{
// We can't reload anything !
return;
}
m_defaultActions.clear();
load(desktopFilesPaths);
}
bool MIMECache::load(const list<string> &desktopFilesPaths)
{
map<string, MIMEAction> loadedActions;
GError *pError = NULL;
bool foundActions = false;
GKeyFile *pDefaults = g_key_file_new();
if (pDefaults == NULL)
{
return false;
}
g_key_file_load_from_file(pDefaults, (const gchar *)m_file.c_str(),
G_KEY_FILE_NONE, &pError);
if (pError == NULL)
{
#ifdef DEBUG
clog << "MIMECache::load: loaded " << m_file << endl;
#endif
gchar **pMimeTypes = g_key_file_get_keys(pDefaults, m_section.c_str(),
NULL, &pError);
if (pMimeTypes != NULL)
{
if (pError == NULL)
{
for (unsigned int i = 0; pMimeTypes[i] != NULL; ++i)
{
gchar **pDesktopFiles = g_key_file_get_string_list(pDefaults,
m_section.c_str(), pMimeTypes[i], NULL, &pError);
if (pDesktopFiles == NULL)
{
continue;
}
if (pError != NULL)
{
g_error_free(pError);
continue;
}
// Register all applications for that type
for (unsigned int j = 0; pDesktopFiles[j] != NULL; ++j)
{
// Search the paths for this desktop file
for (list<string>::const_iterator iter = desktopFilesPaths.begin();
iter != desktopFilesPaths.end(); ++iter)
{
if (findDesktopFile(*iter + pDesktopFiles[j], pMimeTypes[i],
loadedActions) == true)
{
foundActions = true;
break;
}
}
}
g_strfreev(pDesktopFiles);
}
}
else
{
g_error_free(pError);
}
g_strfreev(pMimeTypes);
}
}
else
{
#ifdef DEBUG
clog << "MIMECache::load: failed to load " << m_file << endl;
#endif
g_error_free(pError);
}
g_key_file_free(pDefaults);
return foundActions;
}
pthread_mutex_t MIMEScanner::m_xdgMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_rwlock_t MIMEScanner::m_cachesLock = PTHREAD_RWLOCK_INITIALIZER;
list<MIMECache> MIMEScanner::m_caches;
#endif
MIMEScanner::MIMEScanner()
{
}
MIMEScanner::~MIMEScanner()
{
}
bool MIMEScanner::initialize(const string &userPrefix, const string &systemPrefix)
{
#ifdef USE_GIO
// Initialize the GType system
g_type_init();
return true;
#else
list<string> desktopFilesPaths;
string userDirectory(userPrefix + APPLICATIONS_DIRECTORY);
string systemDirectory(systemPrefix + APPLICATIONS_DIRECTORY);
bool foundActions = false;
// This may be a re-initialize
if (pthread_rwlock_wrlock(&m_cachesLock) == 0)
{
// Empty the caches list
m_caches.clear();
pthread_rwlock_unlock(&m_cachesLock);
}
if (systemDirectory.empty() == false)
{
// User-specific actions may point to desktop files in this path, so add it in
desktopFilesPaths.push_front(systemDirectory);
}
// Load user-specific settings first
if ((userPrefix.empty() == false) &&
(userDirectory.empty() == false))
{
// Add the user's directory to the paths list
desktopFilesPaths.push_front(userDirectory);
#ifdef DEBUG
clog << "MIMEScanner::initialize: user-specific directory " << userDirectory << endl;
#endif
foundActions |= addCache(userDirectory + MIME_DEFAULTS_LIST,
MIME_DEFAULTS_SECTION, desktopFilesPaths);
foundActions |= addCache(userDirectory + MIME_CACHE,
MIME_CACHE_SECTION, desktopFilesPaths);
}
// Then load system-wide settings
if ((systemPrefix.empty() == false) &&
(systemDirectory.empty() == false))
{
// Make sure only the system directory is in the list
desktopFilesPaths.clear();
desktopFilesPaths.push_front(systemDirectory);
#ifdef DEBUG
clog << "MIMEScanner::initialize: system-wide directory " << systemDirectory << endl;
#endif
foundActions |= addCache(systemDirectory + MIME_DEFAULTS_LIST,
MIME_DEFAULTS_SECTION, desktopFilesPaths);
foundActions |= addCache(systemDirectory + MIME_CACHE,
MIME_CACHE_SECTION, desktopFilesPaths);
}
return foundActions;
#endif
}
#ifndef USE_GIO
bool MIMEScanner::addCache(const string &file, const string §ion,
const list<string> &desktopFilesPaths)
{
bool addedCache = false;
if (pthread_rwlock_wrlock(&m_cachesLock) == 0)
{
m_caches.push_back(MIMECache(file, section));
addedCache = m_caches.back().load(desktopFilesPaths);
pthread_rwlock_unlock(&m_cachesLock);
}
return addedCache;
}
#endif
void MIMEScanner::shutdown(void)
{
#ifndef USE_GIO
xdg_mime_shutdown();
#endif
}
void MIMEScanner::listConfigurationFiles(const string &prefix, set<string> &files)
{
if (prefix.empty() == false)
{
files.insert(prefix + APPLICATIONS_DIRECTORY + MIME_DEFAULTS_LIST);
files.insert(prefix + APPLICATIONS_DIRECTORY + MIME_CACHE);
}
}
string MIMEScanner::scanFileType(const string &fileName)
{
if (fileName.empty() == true)
{
return "";
}
// Does it have an obvious extension ?
#ifdef USE_GIO
char *pType = g_content_type_guess(fileName.c_str(), NULL, 0, NULL);
#else
const char *pType = NULL;
if (pthread_mutex_lock(&m_xdgMutex) == 0)
{
pType = xdg_mime_get_mime_type_from_file_name(fileName.c_str());
pthread_mutex_unlock(&m_xdgMutex);
}
#endif
if (pType == NULL)
{
return "";
}
#ifdef USE_GIO
if (g_content_type_is_unknown(pType) == TRUE)
{
g_free(pType);
return "";
}
// Get the corresponding MIME type
char *pMimeType = g_content_type_get_mime_type(pType);
if (pMimeType == NULL)
{
g_free(pType);
return "";
}
g_free(pType);
pType = pMimeType;
#else
if (strncasecmp(pType, xdg_mime_type_unknown, strlen(pType)) == 0)
{
return "";
}
#endif
string mimeType(pType);
// Quick and dirty fix to work-around shared-mime-info mistakenly identifying
// HTML files as Mozilla bookmarks
if ((fileName.find(".htm") != string::npos) &&
(strncasecmp(pType, "application/x-mozilla-bookmarks", strlen(pType)) == 0))
{
mimeType = "text/html";
}
#ifdef USE_GIO
g_free(pType);
#endif
#ifdef DEBUG
clog << "MIMEScanner::scanFileType: " << fileName << " " << mimeType << endl;
#endif
return mimeType;
}
/// Finds out the given file's MIME type.
string MIMEScanner::scanFile(const string &fileName)
{
if (fileName.empty() == true)
{
return "";
}
string mimeType(scanFileType(fileName));
if (mimeType.empty() == false)
{
return mimeType;
}
#ifdef USE_GIO
string uri("file://");
uri += fileName;
// GFile will sniff the file if necessary
GFile *pFile = g_file_new_for_uri(uri.c_str());
if (pFile == NULL)
{
return UNKNOWN_MIME_TYPE;
}
GFileInfo *pFileInfo = g_file_query_info(pFile,
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
(GFileQueryInfoFlags)0, NULL, NULL);
if (pFileInfo == NULL)
{
g_object_unref(pFile);
return UNKNOWN_MIME_TYPE;
}
const char *pType = g_file_info_get_content_type(pFileInfo);
if (pType == NULL)
{
g_object_unref(pFileInfo);
g_object_unref(pFile);
return UNKNOWN_MIME_TYPE;
}
// Get the corresponding MIME type
char *pMimeType = g_content_type_get_mime_type(pType);
if (pMimeType == NULL)
{
g_object_unref(pFileInfo);
g_object_unref(pFile);
return UNKNOWN_MIME_TYPE;
}
mimeType = pMimeType;
g_free(pMimeType);
g_object_unref(pFileInfo);
g_object_unref(pFile);
#else
const char *pType = NULL;
// Have a peek at the file
if (pthread_mutex_lock(&m_xdgMutex) == 0)
{
pType = xdg_mime_get_mime_type_for_file(fileName.c_str(), NULL);
pthread_mutex_unlock(&m_xdgMutex);
}
if (pType != NULL)
{
mimeType = pType;
}
else if (xdg_mime_type_unknown != NULL)
{
mimeType = xdg_mime_type_unknown;
}
#endif
#ifdef DEBUG
clog << "MIMEScanner::scanFile: " << fileName << " " << mimeType << endl;
#endif
return mimeType;
}
/// Finds out the given URL's MIME type.
string MIMEScanner::scanUrl(const Url &urlObj)
{
string mimeType;
// Is it a local file ?
if (urlObj.getProtocol() == "file")
{
string fileName(urlObj.getLocation());
fileName += "/";
fileName += urlObj.getFile();
mimeType = scanFile(fileName);
}
else
{
mimeType = scanFileType(urlObj.getFile());
}
if (mimeType.empty() == false)
{
return mimeType;
}
if (urlObj.getProtocol() == "http")
{
mimeType = "text/html";
}
#ifdef USE_GIO
else
{
mimeType = UNKNOWN_MIME_TYPE;
}
#else
else if (xdg_mime_type_unknown != NULL)
{
mimeType = xdg_mime_type_unknown;
}
#endif
#ifdef DEBUG
clog << "MIMEScanner::scanUrl: " << urlObj.getFile() << " " << mimeType << endl;
#endif
return mimeType;
}
/// Finds out the given data buffer's MIME type.
string MIMEScanner::scanData(const char *pData, unsigned int length)
{
if ((pData == NULL) ||
(length == 0))
{
return "";
}
#ifdef USE_GIO
char *pType = g_content_type_guess(NULL, (const guchar *)pData, (gsize)length, NULL);
#else
const char *pType = NULL;
if (pthread_mutex_lock(&m_xdgMutex) == 0)
{
pType = xdg_mime_get_mime_type_for_data((const void *)pData, (size_t)length, NULL);
pthread_mutex_unlock(&m_xdgMutex);
}
#endif
if (pType == NULL)
{
return UNKNOWN_MIME_TYPE;
}
#ifdef USE_GIO
// Get the corresponding MIME type
char *pMimeType = g_content_type_get_mime_type(pType);
if (pMimeType == NULL)
{
g_free(pType);
return UNKNOWN_MIME_TYPE;
}
g_free(pType);
pType = pMimeType;
#endif
string mimeType(pType);
#ifdef USE_GIO
g_free(pType);
#endif
#ifdef DEBUG
clog << "MIMEScanner::scanData: " << mimeType << endl;
#endif
return mimeType;
}
/// Gets parent MIME types.
bool MIMEScanner::getParentTypes(const string &mimeType,
const set<string> &allTypes, set<string> &parentMimeTypes)
{
if (mimeType.empty() == true)
{
return false;
}
#ifdef USE_GIO
for (set<string>::const_iterator typeIter = allTypes.begin(); typeIter != allTypes.end(); ++typeIter)
{
// FIXME: this function deals with content types, not MIME types !
// Check whether g_content_type_from_mime_type() exists
if (g_content_type_is_a(mimeType.c_str(), typeIter->c_str()) == TRUE)
{
#ifdef DEBUG
clog << "MIMEScanner::getParentTypes: " << mimeType << " is a " << *typeIter << endl;
#endif
parentMimeTypes.insert(*typeIter);
}
}
#else
char **pParentTypes = xdg_mime_list_mime_parents(mimeType.c_str());
if ((pParentTypes != NULL) &&
(pParentTypes[0] != NULL))
{
for (unsigned int i = 0; pParentTypes[i] != NULL; ++i)
{
parentMimeTypes.insert(pParentTypes[i]);
}
free(pParentTypes);
return true;
}
#endif
return false;
}
/// Adds a user-defined action for the given type.
void MIMEScanner::addDefaultAction(const string &mimeType, const MIMEAction &typeAction)
{
#ifdef USE_GIO
if (typeAction.m_pAppInfo == NULL)
{
return;
}
GError *pError = NULL;
g_app_info_set_as_default_for_type(typeAction.m_pAppInfo, mimeType.c_str(), &pError);
if (pError != NULL)
{
g_error_free(pError);
}
#else
// Custom actions get stored in a cache object which is not connected to a file
// We need to create this object the first time a custom action gets added
if (pthread_rwlock_wrlock(&m_cachesLock) == 0)
{
if ((m_caches.empty() == true) ||
(m_caches.front().m_file.empty() == false))
{
m_caches.push_front(MIMECache());
}
m_caches.front().m_defaultActions.insert(pair<string, MIMEAction>(mimeType, typeAction));
pthread_rwlock_unlock(&m_cachesLock);
}
#endif
}
bool MIMEScanner::getDefaultActionsForType(const string &mimeType, bool isLocal,
set<string> &actionNames, vector<MIMEAction> &typeActions)
{
#ifdef USE_GIO
// Get default actions first
GAppInfo *pDefAppInfo1 = g_app_info_get_default_for_type(mimeType.c_str(), (isLocal == false ? TRUE : FALSE));
if (pDefAppInfo1 != NULL)
{
MIMEAction action(pDefAppInfo1);
#ifdef DEBUG
clog << "MIMEScanner::getDefaultActionsForType: default action "
<< isLocal << " " << action.m_name << endl;
#endif
actionNames.insert(action.m_name);
typeActions.push_back(action);
g_object_unref(pDefAppInfo1);
}
// Get all other actions
GList *pAppInfoList = g_app_info_get_all_for_type(mimeType.c_str());
if (pAppInfoList == NULL)
{
return !typeActions.empty();
}
typeActions.reserve(g_list_length(pAppInfoList));
for (GList *pList = pAppInfoList; pList != NULL; pList = pList->next)
{
if (pList->data == NULL)
{
continue;
}
GAppInfo *pAppInfo = G_APP_INFO(pList->data);
if (pAppInfo == NULL)
{
continue;
}
MIMEAction action(pAppInfo);
// Skip defaults
if (actionNames.find(action.m_name) == actionNames.end())
{
#ifdef DEBUG
clog << "MIMEScanner::getDefaultActionsForType: action " << action.m_name << endl;
#endif
actionNames.insert(action.m_name);
typeActions.push_back(action);
}
}
g_list_foreach(pAppInfoList, (GFunc)g_object_unref, NULL);
g_list_free(pAppInfoList);
return true;
#else
bool foundAction = false;
if (pthread_rwlock_rdlock(&m_cachesLock) != 0)
{
return false;
}
// Each cache may have more than one action for this type
for (list<MIMECache>::iterator cacheIter = m_caches.begin(); cacheIter != m_caches.end(); ++cacheIter)
{
MIMECache &cache = *cacheIter;
multimap<string, MIMEAction> &defaultActions = cache.m_defaultActions;
multimap<string, MIMEAction>::const_iterator actionIter = defaultActions.lower_bound(mimeType);
multimap<string, MIMEAction>::const_iterator endActionIter = defaultActions.upper_bound(mimeType);
// Found anything ?
for (; actionIter != endActionIter; ++actionIter)
{
// The same action may be defined for another type
if (actionNames.find(actionIter->second.m_name) == actionNames.end())
{
#ifdef DEBUG
clog << "MIMEScanner::getDefaultActionsForType: action " << actionIter->second.m_name
<< " at " << actionIter->second.m_location << endl;
#endif
actionNames.insert(actionIter->second.m_name);
typeActions.push_back(actionIter->second);
foundAction = true;
}
}
}
pthread_rwlock_unlock(&m_cachesLock);
return foundAction;
#endif
}
/// Determines the default action(s) for the given type.
bool MIMEScanner::getDefaultActions(const string &mimeType, bool isLocal,
vector<MIMEAction> &typeActions)
{
set<string> actionNames;
typeActions.clear();
#ifdef DEBUG
clog << "MIMEScanner::getDefaultActions: searching for " << mimeType << endl;
#endif
bool foundAction = getDefaultActionsForType(mimeType, isLocal, actionNames, typeActions);
#ifndef USE_GIO
if (foundAction == false)
{
// Is there an action for any of this type's parents ?
char **pParentTypes = xdg_mime_list_mime_parents(mimeType.c_str());
if ((pParentTypes != NULL) &&
(pParentTypes[0] != NULL))
{
for (unsigned int i = 0; pParentTypes[i] != NULL; ++i)
{
string parentType(pParentTypes[i]);
#ifdef DEBUG
clog << "MIMEScanner::getDefaultActions: searching for parent type " << parentType << endl;
#endif
foundAction = getDefaultActionsForType(parentType, isLocal, actionNames, typeActions);
if (foundAction)
{
break;
}
}
free(pParentTypes);
}
#ifdef DEBUG
else clog << "MIMEScanner::getDefaultActions: " << mimeType << " has no parent types" << endl;
#endif
}
#endif
return foundAction;
}
string MIMEScanner::getDescription(const string &mimeType)
{
#ifdef USE_GIO
char *pDesc = g_content_type_get_description(mimeType.c_str());
if (pDesc != NULL)
{
string description(pDesc);
// Upper-case the first character
description[0] = (char)toupper((int)pDesc[0]);
g_free(pDesc);
return description;
}
#endif
return mimeType;
}
|