~ubuntu-branches/ubuntu/maverick/notecase/maverick

« back to all changes in this revision

Viewing changes to src/_unx/ExecuteFile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mitsuya Shibata
  • Date: 2008-01-18 01:54:05 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080118015405-id2r7kphpxlyjzfi
Tags: 1.7.6-0ubuntu1
* New Upstream Release (Closes LP: #182226)
*  Fix failed assertion when change node level (Closes LP: #137464)
* Exclude help.ncd and any .ncd file from compression (Closes LP: #113959)
* Adapted debian/ directory to upstream deb/ directory
* Add debian/watch file
* Remove debian/README.Debian because it is now unnecessary
* Bump up compat level from 4 to 5
* Updating debian/menu file to use the new menu hierarchy
* Modify debian/control file
*  Bump up Standards-Version field from 3.6.1 to 3.7.3
*  Change Section field from x11 to editors
*  Change Build-Depends debhelper version to >=5, libgtk2.0-dev to >=2.4
*  Move Homepage field from description to regular field

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#if defined(HAVE_GNOME_VFS)
18
18
 #include <libgnomevfs/gnome-vfs.h>
19
19
 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
 
20
#else
 
21
 #include <map>
 
22
 static std::map<std::string, std::string> g_mapExt2Mime;
 
23
 static std::map<std::string, std::string> g_mapMime2Prog;
 
24
 static bool g_bMimeMapsLoaded = false;
20
25
#endif
 
26
 
 
27
void trim(std::string& str);
 
28
std::string GetFileExt(const char *);
 
29
bool LoadMimeTypes(const char *szTypesFile, const char *szProgsFile);
21
30
bool Open(const std::string& path, const std::string& args);
22
31
std::string FileNameFromUTF8(const std::string& fileNameInUTF8);
23
32
 
388
397
        std::string mimeType;
389
398
        std::string strURI;
390
399
 
391
 
        if(std::string::npos != path.find("://"))
 
400
        // first try heuristic - http link would crash in GetMimeType 
 
401
            if (path.substr(0, 7) == "http://")
 
402
                mimeType = "text/html";
 
403
            else if (path.substr(0, 8) == "https://")
 
404
                mimeType = "text/html";
 
405
            else if (path.substr(0, 6) == "ftp://")
 
406
                mimeType = "text/html";
 
407
            else if (path.substr(0, 7) == "mailto:")
 
408
                mimeType = "message/rfc822";
 
409
        else if(std::string::npos != path.find("://"))
392
410
        {
393
411
                GnomeVFS::URI uri(path);
394
412
                mimeType = GnomeVFS::GetMimeType(uri);
408
426
 
409
427
        if (mimeType.empty())
410
428
        {
411
 
            if (path.substr(0, 7) == "http://")
412
 
                mimeType = "text/html";
413
 
            else if (path.substr(0, 8) == "https://")
414
 
                mimeType = "text/html";
415
 
            else if (path.substr(0, 6) == "ftp://")
416
 
                mimeType = "text/html";
417
 
            else if (path.substr(0, 7) == "mailto:")
418
 
                mimeType = "message/rfc822";
419
429
 
420
430
            TRACE("2: Mime type using heuristic: %s\n", mimeType.c_str());
421
431
        }
433
443
        }
434
444
        else
435
445
                return g_spawn_command_line_async(path.c_str(), 0);
436
 
#endif
437
 
 
438
 
        return g_spawn_command_line_async(FileNameFromUTF8(path).c_str(), 0);
439
 
}
440
 
 
 
446
 
 
447
        return g_spawn_command_line_async(FileNameFromUTF8(path).c_str(), 0);
 
448
 
 
449
#else
 
450
 
 
451
        if(!g_bMimeMapsLoaded){
 
452
                LoadMimeTypes("/etc/mime.types", "/etc/mailcap");
 
453
                g_bMimeMapsLoaded = true;
 
454
        }
 
455
 
 
456
        std::string strExt = GetFileExt(path.c_str());
 
457
        if(!strExt.empty()) strExt = strExt.substr(1); // strip "."
 
458
        if(!strExt.empty()){
 
459
                TRACE("Ext: %s\n", strExt.c_str());
 
460
                std::string strMime = g_mapExt2Mime[strExt];
 
461
                if(!strMime.empty()){
 
462
                        TRACE("Mime: %s\n", strMime.c_str());
 
463
                        std::string strProg = g_mapMime2Prog[strMime];
 
464
                        if(strProg.empty()){
 
465
                                //last chance - try alternative mime
 
466
                                // image/jpeg -> image/*
 
467
                                std::string::size_type nPos = strMime.find('/');
 
468
                                if(nPos != std::string::npos){
 
469
                                        strMime = strMime.substr(0, nPos+1);                            
 
470
                                        strMime += "*";
 
471
                                        TRACE("Mime2: %s\n", strMime.c_str());
 
472
                                        strProg = g_mapMime2Prog[strMime];
 
473
                                }
 
474
                        }
 
475
 
 
476
                        if(!strProg.empty()){
 
477
                                TRACE("Prog: %s\n", strProg.c_str());
 
478
                                char szBuffer[2048];
 
479
                                sprintf(szBuffer, strProg.c_str(), path.c_str());
 
480
                                TRACE("Exec: %s\n", szBuffer);
 
481
                                return g_spawn_command_line_async(szBuffer, 0); // TOFIX from utf8?
 
482
                        }
 
483
                }
 
484
        }
 
485
 
 
486
        return g_spawn_command_line_async(FileNameFromUTF8(path).c_str(), 0);
 
487
 
 
488
#endif
 
489
}
 
490
 
 
491
bool LoadMimeTypes(const char *szTypesFile, const char *szProgsFile)
 
492
{
 
493
#ifndef HAVE_GNOME_VFS
 
494
        //load MIME types
 
495
        FILE *pTFile = fopen(szTypesFile, "r");
 
496
        if(NULL == pTFile)
 
497
                return false;
 
498
 
 
499
        char szBuffer[1024];
 
500
        while(NULL != fgets(szBuffer, sizeof(szBuffer), pTFile))
 
501
        {
 
502
                //kill new line char
 
503
                if('\n'== szBuffer[strlen(szBuffer)-1])
 
504
                        szBuffer[strlen(szBuffer)-1] = '\0';
 
505
 
 
506
                if( strlen(szBuffer) > 0 &&
 
507
                        szBuffer[0] != '#')
 
508
                {
 
509
                        std::string strLine = szBuffer;
 
510
                        
 
511
                        //extract mime type
 
512
                        std::string strMime;
 
513
                        std::string::size_type nPos = strLine.find('\t');
 
514
                        if(nPos != std::string::npos)
 
515
                                strMime = strLine.substr(0, nPos);
 
516
                        trim(strMime);
 
517
                
 
518
                        nPos = strLine.find_last_of('\t');
 
519
                        if(nPos != std::string::npos)
 
520
                                strLine = strLine.substr(nPos+1);
 
521
                        
 
522
                        //extract extensions (delimited with space)
 
523
                        if(!strMime.empty()){
 
524
                                while(std::string::npos != (nPos = strLine.find(' ')))
 
525
                                {
 
526
                                        std::string strExt = strLine.substr(0, nPos);
 
527
                                        g_mapExt2Mime[strExt] = strMime;
 
528
                                        TRACE("Mime [%s]=[%s]\n", strExt.c_str(), strMime.c_str());
 
529
                                        strLine = strLine.substr(nPos+1);
 
530
                                }
 
531
                                if(!strLine.empty()){
 
532
                                        g_mapExt2Mime[strLine] = strMime;
 
533
                                        TRACE("Mime [%s]=[%s]\n", strLine.c_str(), strMime.c_str());
 
534
                                }
 
535
                        }
 
536
                }
 
537
        }
 
538
        fclose(pTFile);
 
539
 
 
540
        //load MIME handler programs
 
541
        FILE *pPFile = fopen(szProgsFile, "r");
 
542
        if(NULL == pPFile)
 
543
                return false;
 
544
 
 
545
        while(NULL != fgets(szBuffer, sizeof(szBuffer), pPFile))
 
546
        {
 
547
                //kill new line char
 
548
                if('\n'== szBuffer[strlen(szBuffer)-1])
 
549
                        szBuffer[strlen(szBuffer)-1] = '\0';
 
550
 
 
551
                if( strlen(szBuffer) > 0 &&
 
552
                        szBuffer[0] != '#')
 
553
                {
 
554
                        std::string strLine = szBuffer;
 
555
                        
 
556
                        //extract mime type
 
557
                        std::string strMime;
 
558
                        std::string::size_type nPos = strLine.find("; ");
 
559
                        if(nPos != std::string::npos){
 
560
                                strMime = strLine.substr(0, nPos);
 
561
                                trim(strMime);
 
562
                                strLine = strLine.substr(nPos+2);
 
563
                                nPos = strLine.find("; ");
 
564
                                if(nPos != std::string::npos)
 
565
                                        strLine = strLine.substr(0, nPos);
 
566
 
 
567
                                g_mapMime2Prog[strMime] = strLine;
 
568
                                TRACE("Prog [%s]=[%s]\n", strMime.c_str(), strLine.c_str());
 
569
                        }
 
570
                }
 
571
        }
 
572
        fclose(pPFile);
 
573
#endif
 
574
        return true;
 
575
}
 
576
 
 
577
 
 
578
void trim(std::string& str)
 
579
{
 
580
  std::string::size_type pos = str.find_last_not_of(' ');
 
581
  if(pos != std::string::npos) {
 
582
    str.erase(pos + 1);
 
583
    pos = str.find_first_not_of(' ');
 
584
    if(pos != std::string::npos) str.erase(0, pos);
 
585
  }
 
586
  else str.erase(str.begin(), str.end());
 
587
}