protected function structure_part($part, $count = 0, $parent = '')
    {
        $struct = new rcube_message_part;
        $struct->mime_id          = $part->mime_id ?: (empty($parent) ? (string)$count : "$parent.$count");
        $struct->headers          = $part->headers;
        $struct->mimetype         = $part->ctype_primary . '/' . $part->ctype_secondary;
        $struct->ctype_primary    = $part->ctype_primary;
        $struct->ctype_secondary  = $part->ctype_secondary;
        $struct->ctype_parameters = $part->ctype_parameters;

        if ($part->headers['content-transfer-encoding']) {
            $struct->encoding = $part->headers['content-transfer-encoding'];
        }

        if ($part->ctype_parameters['charset']) {
            $struct->charset = $part->ctype_parameters['charset'];
        }

        $part_charset = $struct->charset ?: $this->params['default_charset'];

        // determine filename
        if (($filename = $part->d_parameters['filename']) || ($filename = $part->ctype_parameters['name'])) {
            if (!$this->params['decode_headers']) {
                $filename = $this->decodeHeader($filename);
            }

            $struct->filename = $filename;
        }

        $struct->body        = $part->body;
        $struct->size        = strlen($part->body);
        $struct->disposition = $part->disposition;

        $count = 0;
        foreach ((array)$part->parts as $child_part) {
            $struct->parts[] = $this->structure_part($child_part, ++$count, $struct->mime_id);
        }

        return $struct;
    }