summaryrefslogtreecommitdiffstats
path: root/src/chunk.hh
blob: 30d224b0e4f01e45741d712099fdd8ad4f609f77 (plain)
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
# pragma once

# include <vector>
# include <map>
# include <atomic>
# include <string>

# include <gmime/gmime.h>

# include "astroid.hh"
# include "crypto.hh"
# include "proto.hh"

namespace Astroid {
  class Chunk : public Glib::Object {
    friend Crypto;

    protected:
      static std::atomic<uint> nextid;

    public:
      Chunk (GMimeObject *, bool encrypted = false, bool _signed = false, refptr<Crypto> _cr = refptr<Crypto> ());
      ~Chunk ();

      int id;

      /* Chunk assumes ownership of these */
      GMimeObject *       mime_object = NULL;
      GMimeContentType *  content_type;
      ustring content_id;

      ustring get_content_type ();
      bool    is_content_type (const char* major, const char* minor);

      ustring viewable_text (bool, bool verbose = false);

      std::vector<refptr<Chunk>> kids;
      std::vector<refptr<Chunk>> siblings;
      refptr<Chunk> get_by_id (int, bool check_siblings = true);

      bool any_kids_viewable ();
      bool any_kids_viewable_and_preferred ();

      bool viewable   = false;
      bool attachment = false;
      bool preferred  = false;
      bool mime_message = false;
      bool isencrypted  = false;
      bool issigned     = false;

      refptr<Message> get_mime_message ();

      std::map<ustring, GMimeContentType *> viewable_types = {
        { "plain", g_mime_content_type_new ("text", "plain") },
        { "html" , g_mime_content_type_new ("text", "html") }
      };

      GMimeContentType * preferred_type;

      refptr<Crypto> crypt;

      /* attachment specific stuff */
      ustring get_filename ();
      size_t  get_file_size ();
      refptr<Glib::ByteArray> contents ();

      bool save_to (std::string filename, bool overwrite = false);
      void open ();
      void save ();

    private:
      ustring _fname;
      void do_open (ustring);
  };
}