Browse code

core: new AmMimeMody methods - added public method deletePart(const string& content_type) - added private methods clearPart(Parts::iterator position) and convertToSinglepart()

Juha Heinanen authored on 08/05/2014 11:47:20
Showing 2 changed files
... ...
@@ -137,6 +137,11 @@ void AmMimeBody::clearParts()
137 137
   }
138 138
 }
139 139
 
140
+void AmMimeBody::clearPart(Parts::iterator position)
141
+{
142
+  parts.erase(position);
143
+}
144
+
140 145
 void AmMimeBody::clearPayload()
141 146
 {
142 147
   delete [] payload;
... ...
@@ -602,6 +607,17 @@ void AmMimeBody::convertToMultipart()
602 607
   content_len = 0;
603 608
 }
604 609
 
610
+void AmMimeBody::convertToSinglepart()
611
+{
612
+  if (parts.size() == 1) {
613
+    this->ct = parts.front()->ct;
614
+    setPayload(parts.front()->payload, parts.front()->content_len);
615
+    clearParts();
616
+  } else {
617
+    DBG("body does not have exactly one part\n");
618
+  }
619
+}
620
+
605 621
 void AmContentType::setType(const string& t)
606 622
 {
607 623
   type = t;
... ...
@@ -659,6 +675,26 @@ AmMimeBody* AmMimeBody::addPart(const string& content_type)
659 675
   return body;
660 676
 }
661 677
 
678
+int AmMimeBody::deletePart(const string& content_type)
679
+{
680
+  if (!ct.isType(MULTIPART)) {
681
+    DBG("body is not multipart\n");
682
+    return -1;
683
+  }
684
+
685
+  for(Parts::iterator it = parts.begin();
686
+      it != parts.end(); ++it) {
687
+    if((*it)->hasContentType(content_type)) {
688
+      clearPart(it);
689
+      if (parts.size() == 1) convertToSinglepart();
690
+      return 0;
691
+    }
692
+  }
693
+
694
+  DBG("no match");
695
+  return -1;
696
+}
697
+
662 698
 void AmMimeBody::setPayload(const unsigned char* buf, unsigned int len)
663 699
 {
664 700
   if(payload)
... ...
@@ -83,6 +83,7 @@ private:
83 83
   Parts parts;
84 84
 
85 85
   void clearParts();
86
+  void clearPart(Parts::iterator position);
86 87
   void clearPayload();
87 88
 
88 89
   int parseMultipart(const unsigned char* buf, unsigned int len);
... ...
@@ -90,6 +91,7 @@ private:
90 91
   int parseSinglePart(unsigned char* buf, unsigned int len);
91 92
 
92 93
   void convertToMultipart();
94
+  void convertToSinglepart();
93 95
 
94 96
 public:
95 97
   /** Empty constructor */
... ...
@@ -122,6 +124,11 @@ public:
122 124
    */
123 125
   AmMimeBody* addPart(const string& content_type);
124 126
 
127
+  /** 
128
+   * Delete a body part, converting resulting body to single-part if necessary.
129
+   */
130
+  int deletePart(const string& content_type);
131
+
125 132
   /** Get content-type without any parameters */
126 133
   string getCTStr() const { return ct.getStr(); }
127 134