Blame view

libmu_cpp/mailcap.cc 2.83 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
Sergey Poznyakoff authored
2
   Copyright (C) 2004, 2006-2007, 2009-2012, 2014-2017 Free Software
3
   Foundation, Inc.
4 5 6 7

   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
8
   version 3 of the License, or (at your option) any later version.
9 10 11 12 13 14

   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.

15
   You should have received a copy of the GNU Lesser General
16 17
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */
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

#include <mailutils/cpp/mailcap.h>

using namespace mailutils;

//
// Mailcap
//

Mailcap :: Mailcap (const Stream& stm)
{
  int status = mu_mailcap_create (&mailcap, stm.stm);
  if (status)
    throw Exception ("Mailcap::Mailcap", status);
}

Mailcap :: Mailcap (const mu_mailcap_t mailcap)
{
  if (mailcap == 0)
    throw Exception ("Mailcap::Mailcap", EINVAL);

  this->mailcap = mailcap;
}

Mailcap :: ~Mailcap ()
{
  mu_mailcap_destroy (&mailcap);
}

size_t
48
Mailcap :: entries_count ()
49 50 51 52
{
  size_t count = 0;
  int status = mu_mailcap_entries_count (mailcap, &count);
  if (status)
53
    throw Exception ("Mailcap::entries_count", status);
54 55 56 57
  return count;
}

MailcapEntry&
58
Mailcap :: get_entry (size_t i)
59 60 61 62 63
{
  mu_mailcap_entry_t c_entry;

  int status = mu_mailcap_get_entry (mailcap, i, &c_entry);
  if (status)
64
    throw Exception ("Mailcap::get_entry", status);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  MailcapEntry* entry = new MailcapEntry (c_entry);
  return *entry;
}

//
// MailcapEntry
//

MailcapEntry :: MailcapEntry (mu_mailcap_entry_t entry)
{
  if (entry == 0)
    throw Exception ("MailcapEntry::MailcapEntry", EINVAL);

  this->entry = entry;
}

size_t
83
MailcapEntry :: fields_count ()
84 85 86 87
{
  size_t count = 0;
  int status = mu_mailcap_entry_fields_count (entry, &count);
  if (status)
88
    throw Exception ("MailcapEntry::fields_count", status);
89 90 91 92
  return count;
}

std::string
93
MailcapEntry :: get_field (size_t i)
94 95 96 97
{
  int status = mu_mailcap_entry_get_field (entry, i, buf, 
					   sizeof (buf), NULL);
  if (status)
98
    throw Exception ("MailcapEntry::get_field", status);
99 100 101 102
  return std::string (buf);
}

std::string
103
MailcapEntry :: get_typefield ()
104 105 106 107
{
  int status = mu_mailcap_entry_get_typefield (entry, buf,
					       sizeof (buf), NULL);
  if (status)
108
    throw Exception ("MailcapEntry::get_typefield", status);
109 110 111 112
  return std::string (buf);
}

std::string
113
MailcapEntry :: get_viewcommand ()
114 115 116 117
{
  int status = mu_mailcap_entry_get_viewcommand (entry, buf, 
						 sizeof (buf), NULL);
  if (status)
118
    throw Exception ("MailcapEntry::get_viewcommand", status);
119 120 121
  return std::string (buf);
}